
<!DOCTYPE html>

<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>pymatgen.analysis.gb.grain &#8212; pymatgen 2020.7.3 documentation</title>
    <link rel="stylesheet" href="../../../../_static/basic.css" type="text/css" />
    <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
    <script id="documentation_options" data-url_root="../../../../" src="../../../../_static/documentation_options.js"></script>
    <script src="../../../../_static/jquery.js"></script>
    <script src="../../../../_static/underscore.js"></script>
    <script src="../../../../_static/doctools.js"></script>
    <script src="../../../../_static/language_data.js"></script>
    <script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    <link rel="index" title="Index" href="../../../../genindex.html" />
    <link rel="search" title="Search" href="../../../../search.html" />
 
<script type="text/javascript">
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-33990148-1']);
  _gaq.push(['_trackPageview']);
</script>

  </head><body>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../../index.html">pymatgen 2020.7.3 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../../index.html" >Module code</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="../../../pymatgen.html" accesskey="U">pymatgen</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="">pymatgen.analysis.gb.grain</a></li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
          <div class="body" role="main">
            
  <h1>Source code for pymatgen.analysis.gb.grain</h1><div class="highlight"><pre>
<span></span><span class="c1"># coding: utf-8</span>
<span class="c1"># Copyright (c) Pymatgen Development Team.</span>
<span class="c1"># Distributed under the terms of the MIT License.</span>

<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">Module containing classes to generate grain boundaries.</span>
<span class="sd">&quot;&quot;&quot;</span>

<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">from</span> <span class="nn">fractions</span> <span class="kn">import</span> <span class="n">Fraction</span>
<span class="kn">from</span> <span class="nn">math</span> <span class="kn">import</span> <span class="n">gcd</span><span class="p">,</span> <span class="n">floor</span><span class="p">,</span> <span class="n">cos</span>
<span class="kn">from</span> <span class="nn">functools</span> <span class="kn">import</span> <span class="n">reduce</span>
<span class="kn">from</span> <span class="nn">pymatgen</span> <span class="kn">import</span> <span class="n">Structure</span><span class="p">,</span> <span class="n">Lattice</span>
<span class="kn">from</span> <span class="nn">pymatgen.core.sites</span> <span class="kn">import</span> <span class="n">PeriodicSite</span>
<span class="kn">from</span> <span class="nn">monty.fractions</span> <span class="kn">import</span> <span class="n">lcm</span>
<span class="kn">from</span> <span class="nn">pymatgen.symmetry.analyzer</span> <span class="kn">import</span> <span class="n">SpacegroupAnalyzer</span>
<span class="kn">import</span> <span class="nn">itertools</span>

<span class="kn">import</span> <span class="nn">logging</span>
<span class="kn">import</span> <span class="nn">warnings</span>

<span class="c1"># This module implements representations of grain boundaries, as well as</span>
<span class="c1"># algorithms for generating them.</span>

<span class="n">__author__</span> <span class="o">=</span> <span class="s2">&quot;Xiang-Guo Li&quot;</span>
<span class="n">__copyright__</span> <span class="o">=</span> <span class="s2">&quot;Copyright 2018, The Materials Virtual Lab&quot;</span>
<span class="n">__version__</span> <span class="o">=</span> <span class="s2">&quot;0.1&quot;</span>
<span class="n">__maintainer__</span> <span class="o">=</span> <span class="s2">&quot;Xiang-Guo Li&quot;</span>
<span class="n">__email__</span> <span class="o">=</span> <span class="s2">&quot;xil110@ucsd.edu&quot;</span>
<span class="n">__date__</span> <span class="o">=</span> <span class="s2">&quot;7/30/18&quot;</span>

<span class="n">logger</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="vm">__name__</span><span class="p">)</span>


<div class="viewcode-block" id="GrainBoundary"><a class="viewcode-back" href="../../../../pymatgen.analysis.gb.grain.html#pymatgen.analysis.gb.grain.GrainBoundary">[docs]</a><span class="k">class</span> <span class="nc">GrainBoundary</span><span class="p">(</span><span class="n">Structure</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Subclass of Structure representing a GrainBoundary (gb) object.</span>
<span class="sd">    Implements additional attributes pertaining to gbs, but the</span>
<span class="sd">    init method does not actually implement any algorithm that</span>
<span class="sd">    creates a gb. This is a DUMMY class who&#39;s init method only holds</span>
<span class="sd">    information about the gb. Also has additional methods that returns</span>
<span class="sd">    other information about a gb such as sigma value.</span>

<span class="sd">    Note that all gbs have the gb surface normal oriented in the c-direction.</span>
<span class="sd">    This means the lattice vectors a and b are in the gb surface plane (at</span>
<span class="sd">     least for one grain) and the c vector is out of the surface plane</span>
<span class="sd">     (though not necessary perpendicular to the surface.)</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">lattice</span><span class="p">,</span> <span class="n">species</span><span class="p">,</span> <span class="n">coords</span><span class="p">,</span> <span class="n">rotation_axis</span><span class="p">,</span> <span class="n">rotation_angle</span><span class="p">,</span>
                 <span class="n">gb_plane</span><span class="p">,</span> <span class="n">join_plane</span><span class="p">,</span> <span class="n">init_cell</span><span class="p">,</span> <span class="n">vacuum_thickness</span><span class="p">,</span> <span class="n">ab_shift</span><span class="p">,</span>
                 <span class="n">site_properties</span><span class="p">,</span> <span class="n">oriented_unit_cell</span><span class="p">,</span> <span class="n">validate_proximity</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
                 <span class="n">coords_are_cartesian</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Makes a gb structure, a structure object with additional information</span>
<span class="sd">        and methods pertaining to gbs.</span>

<span class="sd">        Args:</span>
<span class="sd">            lattice (Lattice/3x3 array): The lattice, either as a</span>
<span class="sd">                :class:`pymatgen.core.lattice.Lattice` or</span>
<span class="sd">                simply as any 2D array. Each row should correspond to a lattice</span>
<span class="sd">                vector. E.g., [[10,0,0], [20,10,0], [0,0,30]] specifies a</span>
<span class="sd">                lattice with lattice vectors [10,0,0], [20,10,0] and [0,0,30].</span>
<span class="sd">            species ([Specie]): Sequence of species on each site. Can take in</span>
<span class="sd">                flexible input, including:</span>

<span class="sd">                i.  A sequence of element / specie specified either as string</span>
<span class="sd">                    symbols, e.g. [&quot;Li&quot;, &quot;Fe2+&quot;, &quot;P&quot;, ...] or atomic numbers,</span>
<span class="sd">                    e.g., (3, 56, ...) or actual Element or Specie objects.</span>

<span class="sd">                ii. List of dict of elements/species and occupancies, e.g.,</span>
<span class="sd">                    [{&quot;Fe&quot; : 0.5, &quot;Mn&quot;:0.5}, ...]. This allows the setup of</span>
<span class="sd">                    disordered structures.</span>
<span class="sd">            coords (Nx3 array): list of fractional/cartesian coordinates of</span>
<span class="sd">                each species.</span>
<span class="sd">            rotation_axis (list): Rotation axis of GB in the form of a list of</span>
<span class="sd">                integers, e.g. [1, 1, 0].</span>
<span class="sd">            rotation_angle (float, in unit of degree): rotation angle of GB.</span>
<span class="sd">            gb_plane (list): Grain boundary plane in the form of a list of integers</span>
<span class="sd">                e.g.: [1, 2, 3].</span>
<span class="sd">            join_plane (list): Joining plane of the second grain in the form of a list of</span>
<span class="sd">                integers. e.g.: [1, 2, 3].</span>
<span class="sd">            init_cell (Structure): initial bulk structure to form the GB.</span>
<span class="sd">            site_properties (dict): Properties associated with the sites as a</span>
<span class="sd">                dict of sequences, The sequences have to be the same length as</span>
<span class="sd">                the atomic species and fractional_coords. For gb, you should</span>
<span class="sd">                have the &#39;grain_label&#39; properties to classify the sites as &#39;top&#39;,</span>
<span class="sd">                &#39;bottom&#39;, &#39;top_incident&#39;, or &#39;bottom_incident&#39;.</span>
<span class="sd">            vacuum_thickness (float in angstrom): The thickness of vacuum inserted</span>
<span class="sd">                between two grains of the GB.</span>
<span class="sd">            ab_shift (list of float, in unit of crystal vector a, b): The relative</span>
<span class="sd">                shift along a, b vectors.</span>
<span class="sd">            oriented_unit_cell (Structure): oriented unit cell of the bulk init_cell.</span>
<span class="sd">                Help to accurate calculate the bulk properties that are consistent</span>
<span class="sd">                with gb calculations.</span>
<span class="sd">            validate_proximity (bool): Whether to check if there are sites</span>
<span class="sd">                that are less than 0.01 Ang apart. Defaults to False.</span>
<span class="sd">            coords_are_cartesian (bool): Set to True if you are providing</span>
<span class="sd">                coordinates in cartesian coordinates. Defaults to False.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">oriented_unit_cell</span> <span class="o">=</span> <span class="n">oriented_unit_cell</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">rotation_axis</span> <span class="o">=</span> <span class="n">rotation_axis</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">rotation_angle</span> <span class="o">=</span> <span class="n">rotation_angle</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">gb_plane</span> <span class="o">=</span> <span class="n">gb_plane</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">join_plane</span> <span class="o">=</span> <span class="n">join_plane</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">init_cell</span> <span class="o">=</span> <span class="n">init_cell</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">vacuum_thickness</span> <span class="o">=</span> <span class="n">vacuum_thickness</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ab_shift</span> <span class="o">=</span> <span class="n">ab_shift</span>
        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span>
            <span class="n">lattice</span><span class="p">,</span> <span class="n">species</span><span class="p">,</span> <span class="n">coords</span><span class="p">,</span> <span class="n">validate_proximity</span><span class="o">=</span><span class="n">validate_proximity</span><span class="p">,</span>
            <span class="n">coords_are_cartesian</span><span class="o">=</span><span class="n">coords_are_cartesian</span><span class="p">,</span>
            <span class="n">site_properties</span><span class="o">=</span><span class="n">site_properties</span><span class="p">)</span>

<div class="viewcode-block" id="GrainBoundary.copy"><a class="viewcode-back" href="../../../../pymatgen.analysis.gb.grain.html#pymatgen.analysis.gb.grain.GrainBoundary.copy">[docs]</a>    <span class="k">def</span> <span class="nf">copy</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Convenience method to get a copy of the structure, with options to add</span>
<span class="sd">        site properties.</span>

<span class="sd">        Returns:</span>
<span class="sd">            A copy of the Structure, with optionally new site_properties and</span>
<span class="sd">            optionally sanitized.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">GrainBoundary</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">lattice</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">species_and_occu</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">frac_coords</span><span class="p">,</span>
                             <span class="bp">self</span><span class="o">.</span><span class="n">rotation_axis</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">rotation_angle</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">gb_plane</span><span class="p">,</span>
                             <span class="bp">self</span><span class="o">.</span><span class="n">join_plane</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">init_cell</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">vacuum_thickness</span><span class="p">,</span>
                             <span class="bp">self</span><span class="o">.</span><span class="n">ab_shift</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">site_properties</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">oriented_unit_cell</span><span class="p">)</span></div>

<div class="viewcode-block" id="GrainBoundary.get_sorted_structure"><a class="viewcode-back" href="../../../../pymatgen.analysis.gb.grain.html#pymatgen.analysis.gb.grain.GrainBoundary.get_sorted_structure">[docs]</a>    <span class="k">def</span> <span class="nf">get_sorted_structure</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">reverse</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get a sorted copy of the structure. The parameters have the same</span>
<span class="sd">        meaning as in list.sort. By default, sites are sorted by the</span>
<span class="sd">        electronegativity of the species. Note that Slab has to override this</span>
<span class="sd">        because of the different __init__ args.</span>
<span class="sd">        Args:</span>
<span class="sd">            key: Specifies a function of one argument that is used to extract</span>
<span class="sd">                a comparison key from each list element: key=str.lower. The</span>
<span class="sd">                default value is None (compare the elements directly).</span>
<span class="sd">            reverse (bool): If set to True, then the list elements are sorted</span>
<span class="sd">                as if each comparison were reversed.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">sites</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="o">=</span><span class="n">key</span><span class="p">,</span> <span class="n">reverse</span><span class="o">=</span><span class="n">reverse</span><span class="p">)</span>
        <span class="n">s</span> <span class="o">=</span> <span class="n">Structure</span><span class="o">.</span><span class="n">from_sites</span><span class="p">(</span><span class="n">sites</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">GrainBoundary</span><span class="p">(</span><span class="n">s</span><span class="o">.</span><span class="n">lattice</span><span class="p">,</span> <span class="n">s</span><span class="o">.</span><span class="n">species_and_occu</span><span class="p">,</span> <span class="n">s</span><span class="o">.</span><span class="n">frac_coords</span><span class="p">,</span>
                             <span class="bp">self</span><span class="o">.</span><span class="n">rotation_axis</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">rotation_angle</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">gb_plane</span><span class="p">,</span>
                             <span class="bp">self</span><span class="o">.</span><span class="n">join_plane</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">init_cell</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">vacuum_thickness</span><span class="p">,</span>
                             <span class="bp">self</span><span class="o">.</span><span class="n">ab_shift</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">site_properties</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">oriented_unit_cell</span><span class="p">)</span></div>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">sigma</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        This method returns the sigma value of the gb.</span>
<span class="sd">        If using &#39;quick_gen&#39; to generate GB, this value is not valid.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="nb">int</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">oriented_unit_cell</span><span class="o">.</span><span class="n">volume</span> <span class="o">/</span> <span class="bp">self</span><span class="o">.</span><span class="n">init_cell</span><span class="o">.</span><span class="n">volume</span><span class="p">))</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">sigma_from_site_prop</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        This method returns the sigma value of the gb from site properties.</span>
<span class="sd">        If the GB structure merge some atoms due to the atoms too closer with</span>
<span class="sd">        each other, this property will not work.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">num_coi</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="k">if</span> <span class="kc">None</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">site_properties</span><span class="p">[</span><span class="s1">&#39;grain_label&#39;</span><span class="p">]:</span>
            <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s1">&#39;Site were merged, this property do not work&#39;</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">tag</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">site_properties</span><span class="p">[</span><span class="s1">&#39;grain_label&#39;</span><span class="p">]:</span>
            <span class="k">if</span> <span class="s1">&#39;incident&#39;</span> <span class="ow">in</span> <span class="n">tag</span><span class="p">:</span>
                <span class="n">num_coi</span> <span class="o">+=</span> <span class="mi">1</span>
        <span class="k">return</span> <span class="nb">int</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">num_sites</span> <span class="o">/</span> <span class="n">num_coi</span><span class="p">))</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">top_grain</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        return the top grain (Structure) of the GB.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">top_sites</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">tag</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">site_properties</span><span class="p">[</span><span class="s1">&#39;grain_label&#39;</span><span class="p">]):</span>
            <span class="k">if</span> <span class="s1">&#39;top&#39;</span> <span class="ow">in</span> <span class="n">tag</span><span class="p">:</span>
                <span class="n">top_sites</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">sites</span><span class="p">[</span><span class="n">i</span><span class="p">])</span>
        <span class="k">return</span> <span class="n">Structure</span><span class="o">.</span><span class="n">from_sites</span><span class="p">(</span><span class="n">top_sites</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">bottom_grain</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        return the bottom grain (Structure) of the GB.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">bottom_sites</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">tag</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">site_properties</span><span class="p">[</span><span class="s1">&#39;grain_label&#39;</span><span class="p">]):</span>
            <span class="k">if</span> <span class="s1">&#39;bottom&#39;</span> <span class="ow">in</span> <span class="n">tag</span><span class="p">:</span>
                <span class="n">bottom_sites</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">sites</span><span class="p">[</span><span class="n">i</span><span class="p">])</span>
        <span class="k">return</span> <span class="n">Structure</span><span class="o">.</span><span class="n">from_sites</span><span class="p">(</span><span class="n">bottom_sites</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">coincidents</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        return the a list of coincident sites.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">coincident_sites</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">tag</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">site_properties</span><span class="p">[</span><span class="s1">&#39;grain_label&#39;</span><span class="p">]):</span>
            <span class="k">if</span> <span class="s1">&#39;incident&#39;</span> <span class="ow">in</span> <span class="n">tag</span><span class="p">:</span>
                <span class="n">coincident_sites</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">sites</span><span class="p">[</span><span class="n">i</span><span class="p">])</span>
        <span class="k">return</span> <span class="n">coincident_sites</span>

    <span class="k">def</span> <span class="fm">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">comp</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">composition</span>
        <span class="n">outs</span> <span class="o">=</span> <span class="p">[</span>
            <span class="s2">&quot;Gb Summary (</span><span class="si">%s</span><span class="s2">)&quot;</span> <span class="o">%</span> <span class="n">comp</span><span class="o">.</span><span class="n">formula</span><span class="p">,</span>
            <span class="s2">&quot;Reduced Formula: </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="n">comp</span><span class="o">.</span><span class="n">reduced_formula</span><span class="p">,</span>
            <span class="s2">&quot;Rotation axis: </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">rotation_axis</span><span class="p">,),</span>
            <span class="s2">&quot;Rotation angle: </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">rotation_angle</span><span class="p">,),</span>
            <span class="s2">&quot;GB plane: </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">gb_plane</span><span class="p">,),</span>
            <span class="s2">&quot;Join plane: </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">join_plane</span><span class="p">,),</span>
            <span class="s2">&quot;vacuum thickness: </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">vacuum_thickness</span><span class="p">,),</span>
            <span class="s2">&quot;ab_shift: </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">ab_shift</span><span class="p">,),</span> <span class="p">]</span>

        <span class="k">def</span> <span class="nf">to_s</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">rjust</span><span class="o">=</span><span class="mi">10</span><span class="p">):</span>
            <span class="k">return</span> <span class="p">(</span><span class="s2">&quot;</span><span class="si">%0.6f</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="n">x</span><span class="p">)</span><span class="o">.</span><span class="n">rjust</span><span class="p">(</span><span class="n">rjust</span><span class="p">)</span>

        <span class="n">outs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s2">&quot;abc   : &quot;</span> <span class="o">+</span> <span class="s2">&quot; &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="n">to_s</span><span class="p">(</span><span class="n">i</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">lattice</span><span class="o">.</span><span class="n">abc</span><span class="p">]))</span>
        <span class="n">outs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s2">&quot;angles: &quot;</span> <span class="o">+</span> <span class="s2">&quot; &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="n">to_s</span><span class="p">(</span><span class="n">i</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">lattice</span><span class="o">.</span><span class="n">angles</span><span class="p">]))</span>
        <span class="n">outs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s2">&quot;Sites (</span><span class="si">{i}</span><span class="s2">)&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">i</span><span class="o">=</span><span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="p">)))</span>
        <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">site</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
            <span class="n">outs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s2">&quot; &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="nb">str</span><span class="p">(</span><span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">),</span> <span class="n">site</span><span class="o">.</span><span class="n">species_string</span><span class="p">,</span> <span class="s2">&quot; &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="n">to_s</span><span class="p">(</span><span class="n">j</span><span class="p">,</span> <span class="mi">12</span><span class="p">)</span> <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="n">site</span><span class="o">.</span><span class="n">frac_coords</span><span class="p">])]))</span>
        <span class="k">return</span> <span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">outs</span><span class="p">)</span>

<div class="viewcode-block" id="GrainBoundary.as_dict"><a class="viewcode-back" href="../../../../pymatgen.analysis.gb.grain.html#pymatgen.analysis.gb.grain.GrainBoundary.as_dict">[docs]</a>    <span class="k">def</span> <span class="nf">as_dict</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns:</span>
<span class="sd">            Dictionary representation of GrainBoundary object</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">d</span> <span class="o">=</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">as_dict</span><span class="p">()</span>
        <span class="n">d</span><span class="p">[</span><span class="s2">&quot;@module&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__module__</span>
        <span class="n">d</span><span class="p">[</span><span class="s2">&quot;@class&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span>
        <span class="n">d</span><span class="p">[</span><span class="s2">&quot;init_cell&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">init_cell</span><span class="o">.</span><span class="n">as_dict</span><span class="p">()</span>
        <span class="n">d</span><span class="p">[</span><span class="s2">&quot;rotation_axis&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">rotation_axis</span>
        <span class="n">d</span><span class="p">[</span><span class="s2">&quot;rotation_angle&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">rotation_angle</span>
        <span class="n">d</span><span class="p">[</span><span class="s2">&quot;gb_plane&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">gb_plane</span>
        <span class="n">d</span><span class="p">[</span><span class="s2">&quot;join_plane&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">join_plane</span>
        <span class="n">d</span><span class="p">[</span><span class="s2">&quot;vacuum_thickness&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">vacuum_thickness</span>
        <span class="n">d</span><span class="p">[</span><span class="s2">&quot;ab_shift&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">ab_shift</span>
        <span class="n">d</span><span class="p">[</span><span class="s2">&quot;oriented_unit_cell&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">oriented_unit_cell</span><span class="o">.</span><span class="n">as_dict</span><span class="p">()</span>
        <span class="k">return</span> <span class="n">d</span></div>

<div class="viewcode-block" id="GrainBoundary.from_dict"><a class="viewcode-back" href="../../../../pymatgen.analysis.gb.grain.html#pymatgen.analysis.gb.grain.GrainBoundary.from_dict">[docs]</a>    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">from_dict</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">d</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Generates a GrainBoundary object from a dictionary created by as_dict().</span>

<span class="sd">        Args:</span>
<span class="sd">            d: dict</span>

<span class="sd">        Returns:</span>
<span class="sd">            GrainBoundary object</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">lattice</span> <span class="o">=</span> <span class="n">Lattice</span><span class="o">.</span><span class="n">from_dict</span><span class="p">(</span><span class="n">d</span><span class="p">[</span><span class="s2">&quot;lattice&quot;</span><span class="p">])</span>
        <span class="n">sites</span> <span class="o">=</span> <span class="p">[</span><span class="n">PeriodicSite</span><span class="o">.</span><span class="n">from_dict</span><span class="p">(</span><span class="n">sd</span><span class="p">,</span> <span class="n">lattice</span><span class="p">)</span> <span class="k">for</span> <span class="n">sd</span> <span class="ow">in</span> <span class="n">d</span><span class="p">[</span><span class="s2">&quot;sites&quot;</span><span class="p">]]</span>
        <span class="n">s</span> <span class="o">=</span> <span class="n">Structure</span><span class="o">.</span><span class="n">from_sites</span><span class="p">(</span><span class="n">sites</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">GrainBoundary</span><span class="p">(</span>
            <span class="n">lattice</span><span class="o">=</span><span class="n">lattice</span><span class="p">,</span>
            <span class="n">species</span><span class="o">=</span><span class="n">s</span><span class="o">.</span><span class="n">species_and_occu</span><span class="p">,</span> <span class="n">coords</span><span class="o">=</span><span class="n">s</span><span class="o">.</span><span class="n">frac_coords</span><span class="p">,</span>
            <span class="n">rotation_axis</span><span class="o">=</span><span class="n">d</span><span class="p">[</span><span class="s2">&quot;rotation_axis&quot;</span><span class="p">],</span>
            <span class="n">rotation_angle</span><span class="o">=</span><span class="n">d</span><span class="p">[</span><span class="s2">&quot;rotation_angle&quot;</span><span class="p">],</span>
            <span class="n">gb_plane</span><span class="o">=</span><span class="n">d</span><span class="p">[</span><span class="s2">&quot;gb_plane&quot;</span><span class="p">],</span>
            <span class="n">join_plane</span><span class="o">=</span><span class="n">d</span><span class="p">[</span><span class="s2">&quot;join_plane&quot;</span><span class="p">],</span>
            <span class="n">init_cell</span><span class="o">=</span><span class="n">Structure</span><span class="o">.</span><span class="n">from_dict</span><span class="p">(</span><span class="n">d</span><span class="p">[</span><span class="s2">&quot;init_cell&quot;</span><span class="p">]),</span>
            <span class="n">vacuum_thickness</span><span class="o">=</span><span class="n">d</span><span class="p">[</span><span class="s2">&quot;vacuum_thickness&quot;</span><span class="p">],</span>
            <span class="n">ab_shift</span><span class="o">=</span><span class="n">d</span><span class="p">[</span><span class="s2">&quot;ab_shift&quot;</span><span class="p">],</span>
            <span class="n">oriented_unit_cell</span><span class="o">=</span><span class="n">Structure</span><span class="o">.</span><span class="n">from_dict</span><span class="p">(</span><span class="n">d</span><span class="p">[</span><span class="s2">&quot;oriented_unit_cell&quot;</span><span class="p">]),</span>
            <span class="n">site_properties</span><span class="o">=</span><span class="n">s</span><span class="o">.</span><span class="n">site_properties</span><span class="p">)</span></div></div>


<div class="viewcode-block" id="GrainBoundaryGenerator"><a class="viewcode-back" href="../../../../pymatgen.analysis.gb.grain.html#pymatgen.analysis.gb.grain.GrainBoundaryGenerator">[docs]</a><span class="k">class</span> <span class="nc">GrainBoundaryGenerator</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    This class is to generate grain boundaries (GBs) from bulk</span>
<span class="sd">    conventional cell (fcc, bcc can from the primitive cell), and works for Cubic,</span>
<span class="sd">    Tetragonal, Orthorhombic, Rhombohedral, and Hexagonal systems.</span>
<span class="sd">    It generate GBs from given parameters, which includes</span>
<span class="sd">    GB plane, rotation axis, rotation angle.</span>

<span class="sd">    This class works for any general GB, including twist, tilt and mixed GBs.</span>
<span class="sd">    The three parameters, rotation axis, GB plane and rotation angle, are</span>
<span class="sd">    sufficient to identify one unique GB. While sometimes, users may not be able</span>
<span class="sd">    to tell what exactly rotation angle is but prefer to use sigma as an parameter,</span>
<span class="sd">    this class also provides the function that is able to return all possible</span>
<span class="sd">    rotation angles for a specific sigma value.</span>
<span class="sd">    The same sigma value (with rotation axis fixed) can correspond to</span>
<span class="sd">    multiple rotation angles.</span>
<span class="sd">    Users can use structure matcher in pymatgen to get rid of the redundant structures.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">initial_structure</span><span class="p">,</span> <span class="n">symprec</span><span class="o">=</span><span class="mf">0.1</span><span class="p">,</span> <span class="n">angle_tolerance</span><span class="o">=</span><span class="mi">1</span><span class="p">):</span>

        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        initial_structure (Structure): Initial input structure. It can</span>
<span class="sd">               be conventional or primitive cell (primitive cell works for bcc and fcc).</span>
<span class="sd">               For fcc and bcc, using conventional cell can lead to a non-primitive</span>
<span class="sd">               grain boundary structure.</span>
<span class="sd">               This code supplies Cubic, Tetragonal, Orthorhombic, Rhombohedral, and</span>
<span class="sd">               Hexagonal systems.</span>
<span class="sd">        symprec (float): Tolerance for symmetry finding. Defaults to 0.1 (the value used</span>
<span class="sd">                in Materials Project), which is for structures with slight deviations</span>
<span class="sd">                from their proper atomic positions (e.g., structures relaxed with</span>
<span class="sd">                electronic structure codes).</span>
<span class="sd">                A smaller value of 0.01 is often used for properly refined</span>
<span class="sd">                structures with atoms in the proper symmetry coordinates.</span>
<span class="sd">                User should make sure the symmetry is what you want.</span>
<span class="sd">        angle_tolerance (float): Angle tolerance for symmetry finding.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">analyzer</span> <span class="o">=</span> <span class="n">SpacegroupAnalyzer</span><span class="p">(</span><span class="n">initial_structure</span><span class="p">,</span> <span class="n">symprec</span><span class="p">,</span> <span class="n">angle_tolerance</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">lat_type</span> <span class="o">=</span> <span class="n">analyzer</span><span class="o">.</span><span class="n">get_lattice_type</span><span class="p">()[</span><span class="mi">0</span><span class="p">]</span>
        <span class="k">if</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">lat_type</span> <span class="o">==</span> <span class="s1">&#39;t&#39;</span><span class="p">):</span>
            <span class="c1"># need to use the conventional cell for tetragonal</span>
            <span class="n">initial_structure</span> <span class="o">=</span> <span class="n">analyzer</span><span class="o">.</span><span class="n">get_conventional_standard_structure</span><span class="p">()</span>
            <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="n">c</span> <span class="o">=</span> <span class="n">initial_structure</span><span class="o">.</span><span class="n">lattice</span><span class="o">.</span><span class="n">abc</span>
            <span class="c1"># c axis of tetragonal structure not in the third direction</span>
            <span class="k">if</span> <span class="nb">abs</span><span class="p">(</span><span class="n">a</span> <span class="o">-</span> <span class="n">b</span><span class="p">)</span> <span class="o">&gt;</span> <span class="n">symprec</span><span class="p">:</span>
                <span class="c1"># a == c, rotate b to the third direction</span>
                <span class="k">if</span> <span class="nb">abs</span><span class="p">(</span><span class="n">a</span> <span class="o">-</span> <span class="n">c</span><span class="p">)</span> <span class="o">&lt;</span> <span class="n">symprec</span><span class="p">:</span>
                    <span class="n">initial_structure</span><span class="o">.</span><span class="n">make_supercell</span><span class="p">([[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">],</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">]])</span>
                <span class="c1"># b == c, rotate a to the third direction</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">initial_structure</span><span class="o">.</span><span class="n">make_supercell</span><span class="p">([[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">],</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">]])</span>
        <span class="k">elif</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">lat_type</span> <span class="o">==</span> <span class="s1">&#39;h&#39;</span><span class="p">):</span>
            <span class="n">alpha</span><span class="p">,</span> <span class="n">beta</span><span class="p">,</span> <span class="n">gamma</span> <span class="o">=</span> <span class="n">initial_structure</span><span class="o">.</span><span class="n">lattice</span><span class="o">.</span><span class="n">angles</span>
            <span class="c1"># c axis is not in the third direction</span>
            <span class="k">if</span> <span class="p">(</span><span class="nb">abs</span><span class="p">(</span><span class="n">gamma</span> <span class="o">-</span> <span class="mi">90</span><span class="p">)</span> <span class="o">&lt;</span> <span class="n">angle_tolerance</span><span class="p">):</span>
                <span class="c1"># alpha = 120 or 60, rotate b, c to a, b vectors</span>
                <span class="k">if</span> <span class="p">(</span><span class="nb">abs</span><span class="p">(</span><span class="n">alpha</span> <span class="o">-</span> <span class="mi">90</span><span class="p">)</span> <span class="o">&gt;</span> <span class="n">angle_tolerance</span><span class="p">):</span>
                    <span class="n">initial_structure</span><span class="o">.</span><span class="n">make_supercell</span><span class="p">([[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">],</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">]])</span>
                <span class="c1"># beta = 120 or 60, rotate c, a to a, b vectors</span>
                <span class="k">elif</span> <span class="p">(</span><span class="nb">abs</span><span class="p">(</span><span class="n">beta</span> <span class="o">-</span> <span class="mi">90</span><span class="p">)</span> <span class="o">&gt;</span> <span class="n">angle_tolerance</span><span class="p">):</span>
                    <span class="n">initial_structure</span><span class="o">.</span><span class="n">make_supercell</span><span class="p">([[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">],</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">]])</span>
        <span class="k">elif</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">lat_type</span> <span class="o">==</span> <span class="s1">&#39;r&#39;</span><span class="p">):</span>
            <span class="c1"># need to use primitive cell for rhombohedra</span>
            <span class="n">initial_structure</span> <span class="o">=</span> <span class="n">analyzer</span><span class="o">.</span><span class="n">get_primitive_standard_structure</span><span class="p">()</span>
        <span class="k">elif</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">lat_type</span> <span class="o">==</span> <span class="s1">&#39;o&#39;</span><span class="p">):</span>
            <span class="c1"># need to use the conventional cell for orthorombic</span>
            <span class="n">initial_structure</span> <span class="o">=</span> <span class="n">analyzer</span><span class="o">.</span><span class="n">get_conventional_standard_structure</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">initial_structure</span> <span class="o">=</span> <span class="n">initial_structure</span>

<div class="viewcode-block" id="GrainBoundaryGenerator.gb_from_parameters"><a class="viewcode-back" href="../../../../pymatgen.analysis.gb.grain.html#pymatgen.analysis.gb.grain.GrainBoundaryGenerator.gb_from_parameters">[docs]</a>    <span class="k">def</span> <span class="nf">gb_from_parameters</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">rotation_axis</span><span class="p">,</span> <span class="n">rotation_angle</span><span class="p">,</span> <span class="n">expand_times</span><span class="o">=</span><span class="mi">4</span><span class="p">,</span> <span class="n">vacuum_thickness</span><span class="o">=</span><span class="mf">0.0</span><span class="p">,</span>
                           <span class="n">ab_shift</span><span class="o">=</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="n">normal</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">ratio</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">plane</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">max_search</span><span class="o">=</span><span class="mi">20</span><span class="p">,</span>
                           <span class="n">tol_coi</span><span class="o">=</span><span class="mf">1.e-8</span><span class="p">,</span> <span class="n">rm_ratio</span><span class="o">=</span><span class="mf">0.7</span><span class="p">,</span> <span class="n">quick_gen</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>

        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Args:</span>
<span class="sd">           rotation_axis (list): Rotation axis of GB in the form of a list of integer</span>
<span class="sd">                e.g.: [1, 1, 0]</span>
<span class="sd">           rotation_angle (float, in unit of degree): rotation angle used to generate GB.</span>
<span class="sd">                Make sure the angle is accurate enough. You can use the enum* functions</span>
<span class="sd">                in this class to extract the accurate angle.</span>
<span class="sd">                e.g.: The rotation angle of sigma 3 twist GB with the rotation axis</span>
<span class="sd">                [1, 1, 1] and GB plane (1, 1, 1) can be 60.000000000 degree.</span>
<span class="sd">                If you do not know the rotation angle, but know the sigma value, we have</span>
<span class="sd">                provide the function get_rotation_angle_from_sigma which is able to return</span>
<span class="sd">                all the rotation angles of sigma value you provided.</span>
<span class="sd">           expand_times (int): The multiple times used to expand one unit grain to larger grain.</span>
<span class="sd">                This is used to tune the grain length of GB to warrant that the two GBs in one</span>
<span class="sd">                cell do not interact with each other. Default set to 4.</span>
<span class="sd">           vacuum_thickness (float, in angstrom): The thickness of vacuum that you want to insert</span>
<span class="sd">                between two grains of the GB. Default to 0.</span>
<span class="sd">            ab_shift (list of float, in unit of a, b vectors of Gb): in plane shift of two grains</span>
<span class="sd">            normal (logic):</span>
<span class="sd">                determine if need to require the c axis of top grain (first transformation matrix)</span>
<span class="sd">                perperdicular to the surface or not.</span>
<span class="sd">                default to false.</span>
<span class="sd">            ratio (list of integers):</span>
<span class="sd">                    lattice axial ratio.</span>
<span class="sd">                    For cubic system, ratio is not needed.</span>
<span class="sd">                    For tetragonal system, ratio = [mu, mv], list of two integers,</span>
<span class="sd">                    that is, mu/mv = c2/a2. If it is irrational, set it to none.</span>
<span class="sd">                    For orthorhombic system, ratio = [mu, lam, mv], list of three integers,</span>
<span class="sd">                    that is, mu:lam:mv = c2:b2:a2. If irrational for one axis, set it to None.</span>
<span class="sd">                    e.g. mu:lam:mv = c2,None,a2, means b2 is irrational.</span>
<span class="sd">                    For rhombohedral system, ratio = [mu, mv], list of two integers,</span>
<span class="sd">                    that is, mu/mv is the ratio of (1+2*cos(alpha))/cos(alpha).</span>
<span class="sd">                    If irrational, set it to None.</span>
<span class="sd">                    For hexagonal system, ratio = [mu, mv], list of two integers,</span>
<span class="sd">                    that is, mu/mv = c2/a2. If it is irrational, set it to none.</span>
<span class="sd">                    This code also supplies a class method to generate the ratio from the</span>
<span class="sd">                    structure (get_ratio). User can also make their own approximation and</span>
<span class="sd">                    input the ratio directly.</span>
<span class="sd">            plane (list): Grain boundary plane in the form of a list of integers</span>
<span class="sd">                e.g.: [1, 2, 3]. If none, we set it as twist GB. The plane will be perpendicular</span>
<span class="sd">                to the rotation axis.</span>
<span class="sd">            max_search (int): max search for the GB lattice vectors that give the smallest GB</span>
<span class="sd">                lattice. If normal is true, also max search the GB c vector that perpendicular</span>
<span class="sd">                to the plane. For complex GB, if you want to speed up, you can reduce this value.</span>
<span class="sd">                But too small of this value may lead to error.</span>
<span class="sd">            tol_coi (float): tolerance to find the coincidence sites. When making approximations to</span>
<span class="sd">                the ratio needed to generate the GB, you probably need to increase this tolerance to</span>
<span class="sd">                obtain the correct number of coincidence sites. To check the number of coincidence</span>
<span class="sd">                sites are correct or not, you can compare the generated Gb object&#39;s sigma_from_site_prop</span>
<span class="sd">                with enum* sigma values (what user expected by input).</span>
<span class="sd">            rm_ratio (float): the criteria to remove the atoms which are too close with each other.</span>
<span class="sd">                rm_ratio*bond_length of bulk system is the criteria of bond length, below which the atom</span>
<span class="sd">                will be removed. Default to 0.7.</span>
<span class="sd">            quick_gen (bool): whether to quickly generate a supercell, if set to true, no need to</span>
<span class="sd">                find the smallest cell.</span>

<span class="sd">        Returns:</span>
<span class="sd">           Grain boundary structure (gb object).</span>
<span class="sd">               &quot;&quot;&quot;</span>
        <span class="n">lat_type</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">lat_type</span>
        <span class="c1"># if the initial structure is primitive cell in cubic system,</span>
        <span class="c1"># calculate the transformation matrix from its conventional cell</span>
        <span class="c1"># to primitive cell, basically for bcc and fcc systems.</span>
        <span class="n">trans_cry</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">eye</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">lat_type</span> <span class="o">==</span> <span class="s1">&#39;c&#39;</span><span class="p">:</span>
            <span class="n">analyzer</span> <span class="o">=</span> <span class="n">SpacegroupAnalyzer</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">initial_structure</span><span class="p">)</span>
            <span class="n">convention_cell</span> <span class="o">=</span> <span class="n">analyzer</span><span class="o">.</span><span class="n">get_conventional_standard_structure</span><span class="p">()</span>
            <span class="n">vol_ratio</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">initial_structure</span><span class="o">.</span><span class="n">volume</span> <span class="o">/</span> <span class="n">convention_cell</span><span class="o">.</span><span class="n">volume</span>
            <span class="c1"># bcc primitive cell, belong to cubic system</span>
            <span class="k">if</span> <span class="nb">abs</span><span class="p">(</span><span class="n">vol_ratio</span> <span class="o">-</span> <span class="mf">0.5</span><span class="p">)</span> <span class="o">&lt;</span> <span class="mf">1.e-3</span><span class="p">:</span>
                <span class="n">trans_cry</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([[</span><span class="mf">0.5</span><span class="p">,</span> <span class="mf">0.5</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.5</span><span class="p">],</span> <span class="p">[</span><span class="o">-</span><span class="mf">0.5</span><span class="p">,</span> <span class="mf">0.5</span><span class="p">,</span> <span class="mf">0.5</span><span class="p">],</span> <span class="p">[</span><span class="mf">0.5</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.5</span><span class="p">,</span> <span class="mf">0.5</span><span class="p">]])</span>
                <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;Make sure this is for cubic with bcc primitive cell&quot;</span><span class="p">)</span>
            <span class="c1"># fcc primitive cell, belong to cubic system</span>
            <span class="k">elif</span> <span class="nb">abs</span><span class="p">(</span><span class="n">vol_ratio</span> <span class="o">-</span> <span class="mf">0.25</span><span class="p">)</span> <span class="o">&lt;</span> <span class="mf">1.e-3</span><span class="p">:</span>
                <span class="n">trans_cry</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([[</span><span class="mf">0.5</span><span class="p">,</span> <span class="mf">0.5</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mf">0.5</span><span class="p">,</span> <span class="mf">0.5</span><span class="p">],</span> <span class="p">[</span><span class="mf">0.5</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mf">0.5</span><span class="p">]])</span>
                <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;Make sure this is for cubic with fcc primitive cell&quot;</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;Make sure this is for cubic with conventional cell&quot;</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">lat_type</span> <span class="o">==</span> <span class="s1">&#39;t&#39;</span><span class="p">:</span>
            <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;Make sure this is for tetragonal system&quot;</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">ratio</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;Make sure this is for irrational c2/a2&#39;</span><span class="p">)</span>
            <span class="k">elif</span> <span class="nb">len</span><span class="p">(</span><span class="n">ratio</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">2</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s1">&#39;Tetragonal system needs correct c2/a2 ratio&#39;</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">lat_type</span> <span class="o">==</span> <span class="s1">&#39;o&#39;</span><span class="p">:</span>
            <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;Make sure this is for orthorhombic system&#39;</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">ratio</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s1">&#39;CSL does not exist if all axial ratios are irrational &#39;</span>
                                   <span class="s1">&#39;for an orthorhombic system&#39;</span><span class="p">)</span>
            <span class="k">elif</span> <span class="nb">len</span><span class="p">(</span><span class="n">ratio</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">3</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s1">&#39;Orthorhombic system needs correct c2:b2:a2 ratio&#39;</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">lat_type</span> <span class="o">==</span> <span class="s1">&#39;h&#39;</span><span class="p">:</span>
            <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;Make sure this is for hexagonal system&#39;</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">ratio</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;Make sure this is for irrational c2/a2&#39;</span><span class="p">)</span>
            <span class="k">elif</span> <span class="nb">len</span><span class="p">(</span><span class="n">ratio</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">2</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s1">&#39;Hexagonal system needs correct c2/a2 ratio&#39;</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">lat_type</span> <span class="o">==</span> <span class="s1">&#39;r&#39;</span><span class="p">:</span>
            <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;Make sure this is for rhombohedral system&#39;</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">ratio</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;Make sure this is for irrational (1+2*cos(alpha)/cos(alpha) ratio&#39;</span><span class="p">)</span>
            <span class="k">elif</span> <span class="nb">len</span><span class="p">(</span><span class="n">ratio</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">2</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s1">&#39;Rhombohedral system needs correct &#39;</span>
                                   <span class="s1">&#39;(1+2*cos(alpha)/cos(alpha) ratio&#39;</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s1">&#39;Lattice type not implemented. This code works for cubic, &#39;</span>
                               <span class="s1">&#39;tetragonal, orthorhombic, rhombehedral, hexagonal systems&#39;</span><span class="p">)</span>

        <span class="c1"># transform four index notation to three index notation for hexagonal and rhombohedral</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">rotation_axis</span><span class="p">)</span> <span class="o">==</span> <span class="mi">4</span><span class="p">:</span>
            <span class="n">u1</span> <span class="o">=</span> <span class="n">rotation_axis</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
            <span class="n">v1</span> <span class="o">=</span> <span class="n">rotation_axis</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
            <span class="n">w1</span> <span class="o">=</span> <span class="n">rotation_axis</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span>
            <span class="k">if</span> <span class="n">lat_type</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="o">==</span> <span class="s1">&#39;h&#39;</span><span class="p">:</span>
                <span class="n">u</span> <span class="o">=</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">u1</span> <span class="o">+</span> <span class="n">v1</span>
                <span class="n">v</span> <span class="o">=</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">v1</span> <span class="o">+</span> <span class="n">u1</span>
                <span class="n">w</span> <span class="o">=</span> <span class="n">w1</span>
                <span class="n">rotation_axis</span> <span class="o">=</span> <span class="p">[</span><span class="n">u</span><span class="p">,</span> <span class="n">v</span><span class="p">,</span> <span class="n">w</span><span class="p">]</span>
            <span class="k">elif</span> <span class="n">lat_type</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="o">==</span> <span class="s1">&#39;r&#39;</span><span class="p">:</span>
                <span class="n">u</span> <span class="o">=</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">u1</span> <span class="o">+</span> <span class="n">v1</span> <span class="o">+</span> <span class="n">w1</span>
                <span class="n">v</span> <span class="o">=</span> <span class="n">v1</span> <span class="o">+</span> <span class="n">w1</span> <span class="o">-</span> <span class="n">u1</span>
                <span class="n">w</span> <span class="o">=</span> <span class="n">w1</span> <span class="o">-</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">v1</span> <span class="o">-</span> <span class="n">u1</span>
                <span class="n">rotation_axis</span> <span class="o">=</span> <span class="p">[</span><span class="n">u</span><span class="p">,</span> <span class="n">v</span><span class="p">,</span> <span class="n">w</span><span class="p">]</span>

        <span class="c1"># make sure gcd(rotation_axis)==1</span>
        <span class="k">if</span> <span class="n">reduce</span><span class="p">(</span><span class="n">gcd</span><span class="p">,</span> <span class="n">rotation_axis</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">1</span><span class="p">:</span>
            <span class="n">rotation_axis</span> <span class="o">=</span> <span class="p">[</span><span class="nb">int</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">x</span> <span class="o">/</span> <span class="n">reduce</span><span class="p">(</span><span class="n">gcd</span><span class="p">,</span> <span class="n">rotation_axis</span><span class="p">)))</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">rotation_axis</span><span class="p">]</span>
        <span class="c1"># transform four index notation to three index notation for plane</span>
        <span class="k">if</span> <span class="n">plane</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">plane</span><span class="p">)</span> <span class="o">==</span> <span class="mi">4</span><span class="p">:</span>
                <span class="n">u1</span> <span class="o">=</span> <span class="n">plane</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
                <span class="n">v1</span> <span class="o">=</span> <span class="n">plane</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
                <span class="n">w1</span> <span class="o">=</span> <span class="n">plane</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span>
                <span class="n">plane</span> <span class="o">=</span> <span class="p">[</span><span class="n">u1</span><span class="p">,</span> <span class="n">v1</span><span class="p">,</span> <span class="n">w1</span><span class="p">]</span>
        <span class="c1"># set the plane for grain boundary when plane is None.</span>
        <span class="k">if</span> <span class="n">plane</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">lat_type</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="o">==</span> <span class="s1">&#39;c&#39;</span><span class="p">:</span>
                <span class="n">plane</span> <span class="o">=</span> <span class="n">rotation_axis</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">lat_type</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="o">==</span> <span class="s1">&#39;h&#39;</span><span class="p">:</span>
                    <span class="k">if</span> <span class="n">ratio</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                        <span class="n">c2_a2_ratio</span> <span class="o">=</span> <span class="mi">1</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="n">c2_a2_ratio</span> <span class="o">=</span> <span class="n">ratio</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">/</span> <span class="n">ratio</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
                    <span class="n">metric</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([[</span><span class="mi">1</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.5</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="o">-</span><span class="mf">0.5</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">c2_a2_ratio</span><span class="p">]])</span>
                <span class="k">elif</span> <span class="n">lat_type</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="o">==</span> <span class="s1">&#39;r&#39;</span><span class="p">:</span>
                    <span class="k">if</span> <span class="n">ratio</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                        <span class="n">cos_alpha</span> <span class="o">=</span> <span class="mf">0.5</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="n">cos_alpha</span> <span class="o">=</span> <span class="mf">1.0</span> <span class="o">/</span> <span class="p">(</span><span class="n">ratio</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">/</span> <span class="n">ratio</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">-</span> <span class="mi">2</span><span class="p">)</span>
                    <span class="n">metric</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([[</span><span class="mi">1</span><span class="p">,</span> <span class="n">cos_alpha</span><span class="p">,</span> <span class="n">cos_alpha</span><span class="p">],</span> <span class="p">[</span><span class="n">cos_alpha</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">cos_alpha</span><span class="p">],</span>
                                       <span class="p">[</span><span class="n">cos_alpha</span><span class="p">,</span> <span class="n">cos_alpha</span><span class="p">,</span> <span class="mi">1</span><span class="p">]])</span>
                <span class="k">elif</span> <span class="n">lat_type</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="o">==</span> <span class="s1">&#39;t&#39;</span><span class="p">:</span>
                    <span class="k">if</span> <span class="n">ratio</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                        <span class="n">c2_a2_ratio</span> <span class="o">=</span> <span class="mi">1</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="n">c2_a2_ratio</span> <span class="o">=</span> <span class="n">ratio</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">/</span> <span class="n">ratio</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
                    <span class="n">metric</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">c2_a2_ratio</span><span class="p">]])</span>
                <span class="k">elif</span> <span class="n">lat_type</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="o">==</span> <span class="s1">&#39;o&#39;</span><span class="p">:</span>
                    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">3</span><span class="p">):</span>
                        <span class="k">if</span> <span class="n">ratio</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                            <span class="n">ratio</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span>
                    <span class="n">metric</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="n">ratio</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">/</span> <span class="n">ratio</span><span class="p">[</span><span class="mi">2</span><span class="p">],</span> <span class="mi">0</span><span class="p">],</span>
                                       <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">ratio</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">/</span> <span class="n">ratio</span><span class="p">[</span><span class="mi">2</span><span class="p">]]])</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s1">&#39;Lattice type has not implemented.&#39;</span><span class="p">)</span>

                <span class="n">plane</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">matmul</span><span class="p">(</span><span class="n">rotation_axis</span><span class="p">,</span> <span class="n">metric</span><span class="p">)</span>
                <span class="n">fractions</span> <span class="o">=</span> <span class="p">[</span><span class="n">Fraction</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">.</span><span class="n">limit_denominator</span><span class="p">()</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">plane</span><span class="p">]</span>
                <span class="n">least_mul</span> <span class="o">=</span> <span class="n">reduce</span><span class="p">(</span><span class="n">lcm</span><span class="p">,</span> <span class="p">[</span><span class="n">f</span><span class="o">.</span><span class="n">denominator</span> <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">fractions</span><span class="p">])</span>
                <span class="n">plane</span> <span class="o">=</span> <span class="p">[</span><span class="nb">int</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">x</span> <span class="o">*</span> <span class="n">least_mul</span><span class="p">))</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">plane</span><span class="p">]</span>

        <span class="k">if</span> <span class="n">reduce</span><span class="p">(</span><span class="n">gcd</span><span class="p">,</span> <span class="n">plane</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">1</span><span class="p">:</span>
            <span class="n">index</span> <span class="o">=</span> <span class="n">reduce</span><span class="p">(</span><span class="n">gcd</span><span class="p">,</span> <span class="n">plane</span><span class="p">)</span>
            <span class="n">plane</span> <span class="o">=</span> <span class="p">[</span><span class="nb">int</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">x</span> <span class="o">/</span> <span class="n">index</span><span class="p">))</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">plane</span><span class="p">]</span>

        <span class="n">t1</span><span class="p">,</span> <span class="n">t2</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_trans_mat</span><span class="p">(</span><span class="n">r_axis</span><span class="o">=</span><span class="n">rotation_axis</span><span class="p">,</span> <span class="n">angle</span><span class="o">=</span><span class="n">rotation_angle</span><span class="p">,</span> <span class="n">normal</span><span class="o">=</span><span class="n">normal</span><span class="p">,</span>
                                    <span class="n">trans_cry</span><span class="o">=</span><span class="n">trans_cry</span><span class="p">,</span> <span class="n">lat_type</span><span class="o">=</span><span class="n">lat_type</span><span class="p">,</span> <span class="n">ratio</span><span class="o">=</span><span class="n">ratio</span><span class="p">,</span>
                                    <span class="n">surface</span><span class="o">=</span><span class="n">plane</span><span class="p">,</span> <span class="n">max_search</span><span class="o">=</span><span class="n">max_search</span><span class="p">,</span> <span class="n">quick_gen</span><span class="o">=</span><span class="n">quick_gen</span><span class="p">)</span>

        <span class="c1"># find the join_plane</span>
        <span class="k">if</span> <span class="n">lat_type</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="o">!=</span> <span class="s1">&#39;c&#39;</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">lat_type</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="o">==</span> <span class="s1">&#39;h&#39;</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">ratio</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                    <span class="n">mu</span><span class="p">,</span> <span class="n">mv</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">]</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">mu</span><span class="p">,</span> <span class="n">mv</span> <span class="o">=</span> <span class="n">ratio</span>
                <span class="n">trans_cry1</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="o">-</span><span class="mf">0.5</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="mf">3.0</span><span class="p">)</span> <span class="o">/</span> <span class="mf">2.0</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span>
                                       <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">mu</span> <span class="o">/</span> <span class="n">mv</span><span class="p">)]])</span>
            <span class="k">elif</span> <span class="n">lat_type</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="o">==</span> <span class="s1">&#39;r&#39;</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">ratio</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                    <span class="n">c2_a2_ratio</span> <span class="o">=</span> <span class="mi">1</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">mu</span><span class="p">,</span> <span class="n">mv</span> <span class="o">=</span> <span class="n">ratio</span>
                    <span class="n">c2_a2_ratio</span> <span class="o">=</span> <span class="mf">3.0</span> <span class="o">/</span> <span class="p">(</span><span class="mi">2</span> <span class="o">-</span> <span class="mi">6</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">/</span> <span class="n">mu</span><span class="p">)</span>
                <span class="n">trans_cry1</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([[</span><span class="mf">0.5</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="mf">3.0</span><span class="p">)</span> <span class="o">/</span> <span class="mf">6.0</span><span class="p">,</span> <span class="mf">1.0</span> <span class="o">/</span> <span class="mi">3</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">c2_a2_ratio</span><span class="p">)],</span>
                                       <span class="p">[</span><span class="o">-</span><span class="mf">0.5</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="mf">3.0</span><span class="p">)</span> <span class="o">/</span> <span class="mf">6.0</span><span class="p">,</span> <span class="mf">1.0</span> <span class="o">/</span> <span class="mi">3</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">c2_a2_ratio</span><span class="p">)],</span>
                                       <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="mf">3.0</span><span class="p">)</span> <span class="o">/</span> <span class="mf">3.0</span><span class="p">,</span> <span class="mf">1.0</span> <span class="o">/</span> <span class="mi">3</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">c2_a2_ratio</span><span class="p">)]])</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">lat_type</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="o">==</span> <span class="s1">&#39;t&#39;</span><span class="p">:</span>
                    <span class="k">if</span> <span class="n">ratio</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                        <span class="n">mu</span><span class="p">,</span> <span class="n">mv</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">]</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="n">mu</span><span class="p">,</span> <span class="n">mv</span> <span class="o">=</span> <span class="n">ratio</span>
                    <span class="n">lam</span> <span class="o">=</span> <span class="n">mv</span>
                <span class="k">elif</span> <span class="n">lat_type</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="o">==</span> <span class="s1">&#39;o&#39;</span><span class="p">:</span>
                    <span class="n">new_ratio</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span> <span class="k">if</span> <span class="n">v</span> <span class="ow">is</span> <span class="kc">None</span> <span class="k">else</span> <span class="n">v</span> <span class="k">for</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">ratio</span><span class="p">]</span>
                    <span class="n">mu</span><span class="p">,</span> <span class="n">lam</span><span class="p">,</span> <span class="n">mv</span> <span class="o">=</span> <span class="n">new_ratio</span>
                <span class="n">trans_cry1</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">lam</span> <span class="o">/</span> <span class="n">mv</span><span class="p">),</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">mu</span> <span class="o">/</span> <span class="n">mv</span><span class="p">)]])</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">trans_cry1</span> <span class="o">=</span> <span class="n">trans_cry</span>
        <span class="n">grain_matrix</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">t2</span><span class="p">,</span> <span class="n">trans_cry1</span><span class="p">)</span>
        <span class="n">plane_init</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">cross</span><span class="p">(</span><span class="n">grain_matrix</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">grain_matrix</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
        <span class="k">if</span> <span class="n">lat_type</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="o">!=</span> <span class="s1">&#39;c&#39;</span><span class="p">:</span>
            <span class="n">plane_init</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">plane_init</span><span class="p">,</span> <span class="n">trans_cry1</span><span class="o">.</span><span class="n">T</span><span class="p">)</span>
        <span class="n">join_plane</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">vec_to_surface</span><span class="p">(</span><span class="n">plane_init</span><span class="p">)</span>

        <span class="n">parent_structure</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">initial_structure</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
        <span class="c1"># calculate the bond_length in bulk system.</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">parent_structure</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
            <span class="n">temp_str</span> <span class="o">=</span> <span class="n">parent_structure</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
            <span class="n">temp_str</span><span class="o">.</span><span class="n">make_supercell</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">])</span>
            <span class="n">distance</span> <span class="o">=</span> <span class="n">temp_str</span><span class="o">.</span><span class="n">distance_matrix</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">distance</span> <span class="o">=</span> <span class="n">parent_structure</span><span class="o">.</span><span class="n">distance_matrix</span>
        <span class="n">bond_length</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">min</span><span class="p">(</span><span class="n">distance</span><span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">nonzero</span><span class="p">(</span><span class="n">distance</span><span class="p">)])</span>

        <span class="c1"># top grain</span>
        <span class="n">top_grain</span> <span class="o">=</span> <span class="n">fix_pbc</span><span class="p">(</span><span class="n">parent_structure</span> <span class="o">*</span> <span class="n">t1</span><span class="p">)</span>

        <span class="c1"># obtain the smallest oriended cell</span>
        <span class="k">if</span> <span class="n">normal</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">quick_gen</span><span class="p">:</span>
            <span class="n">t_temp</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_trans_mat</span><span class="p">(</span><span class="n">r_axis</span><span class="o">=</span><span class="n">rotation_axis</span><span class="p">,</span> <span class="n">angle</span><span class="o">=</span><span class="n">rotation_angle</span><span class="p">,</span> <span class="n">normal</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
                                        <span class="n">trans_cry</span><span class="o">=</span><span class="n">trans_cry</span><span class="p">,</span> <span class="n">lat_type</span><span class="o">=</span><span class="n">lat_type</span><span class="p">,</span> <span class="n">ratio</span><span class="o">=</span><span class="n">ratio</span><span class="p">,</span>
                                        <span class="n">surface</span><span class="o">=</span><span class="n">plane</span><span class="p">,</span> <span class="n">max_search</span><span class="o">=</span><span class="n">max_search</span><span class="p">)</span>
            <span class="n">oriended_unit_cell</span> <span class="o">=</span> <span class="n">fix_pbc</span><span class="p">(</span><span class="n">parent_structure</span> <span class="o">*</span> <span class="n">t_temp</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
            <span class="n">t_matrix</span> <span class="o">=</span> <span class="n">oriended_unit_cell</span><span class="o">.</span><span class="n">lattice</span><span class="o">.</span><span class="n">matrix</span>
            <span class="n">normal_v_plane</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">cross</span><span class="p">(</span><span class="n">t_matrix</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">t_matrix</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
            <span class="n">unit_normal_v</span> <span class="o">=</span> <span class="n">normal_v_plane</span> <span class="o">/</span> <span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">norm</span><span class="p">(</span><span class="n">normal_v_plane</span><span class="p">)</span>
            <span class="n">unit_ab_adjust</span> <span class="o">=</span> <span class="p">(</span><span class="n">t_matrix</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">-</span> <span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">unit_normal_v</span><span class="p">,</span> <span class="n">t_matrix</span><span class="p">[</span><span class="mi">2</span><span class="p">])</span> <span class="o">*</span> <span class="n">unit_normal_v</span><span class="p">)</span> \
                <span class="o">/</span> <span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">unit_normal_v</span><span class="p">,</span> <span class="n">t_matrix</span><span class="p">[</span><span class="mi">2</span><span class="p">])</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">oriended_unit_cell</span> <span class="o">=</span> <span class="n">top_grain</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
            <span class="n">unit_ab_adjust</span> <span class="o">=</span> <span class="mf">0.0</span>

        <span class="c1"># bottom grain, using top grain&#39;s lattice matrix</span>
        <span class="n">bottom_grain</span> <span class="o">=</span> <span class="n">fix_pbc</span><span class="p">(</span><span class="n">parent_structure</span> <span class="o">*</span> <span class="n">t2</span><span class="p">,</span> <span class="n">top_grain</span><span class="o">.</span><span class="n">lattice</span><span class="o">.</span><span class="n">matrix</span><span class="p">)</span>

        <span class="c1"># label both grains with &#39;top&#39;,&#39;bottom&#39;,&#39;top_incident&#39;,&#39;bottom_incident&#39;</span>
        <span class="n">n_sites</span> <span class="o">=</span> <span class="n">top_grain</span><span class="o">.</span><span class="n">num_sites</span>
        <span class="n">t_and_b</span> <span class="o">=</span> <span class="n">Structure</span><span class="p">(</span><span class="n">top_grain</span><span class="o">.</span><span class="n">lattice</span><span class="p">,</span> <span class="n">top_grain</span><span class="o">.</span><span class="n">species</span> <span class="o">+</span> <span class="n">bottom_grain</span><span class="o">.</span><span class="n">species</span><span class="p">,</span>
                            <span class="nb">list</span><span class="p">(</span><span class="n">top_grain</span><span class="o">.</span><span class="n">frac_coords</span><span class="p">)</span> <span class="o">+</span> <span class="nb">list</span><span class="p">(</span><span class="n">bottom_grain</span><span class="o">.</span><span class="n">frac_coords</span><span class="p">))</span>
        <span class="n">t_and_b_dis</span> <span class="o">=</span> <span class="n">t_and_b</span><span class="o">.</span><span class="n">lattice</span><span class="o">.</span><span class="n">get_all_distances</span><span class="p">(</span><span class="n">t_and_b</span><span class="o">.</span><span class="n">frac_coords</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="n">n_sites</span><span class="p">],</span>
                                                        <span class="n">t_and_b</span><span class="o">.</span><span class="n">frac_coords</span><span class="p">[</span><span class="n">n_sites</span><span class="p">:</span><span class="n">n_sites</span> <span class="o">*</span> <span class="mi">2</span><span class="p">])</span>
        <span class="n">index_incident</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">nonzero</span><span class="p">(</span><span class="n">t_and_b_dis</span> <span class="o">&lt;</span> <span class="n">np</span><span class="o">.</span><span class="n">min</span><span class="p">(</span><span class="n">t_and_b_dis</span><span class="p">)</span> <span class="o">+</span> <span class="n">tol_coi</span><span class="p">)</span>

        <span class="n">top_labels</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n_sites</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">index_incident</span><span class="p">[</span><span class="mi">0</span><span class="p">]:</span>
                <span class="n">top_labels</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&#39;top_incident&#39;</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">top_labels</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&#39;top&#39;</span><span class="p">)</span>
        <span class="n">bottom_labels</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n_sites</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">index_incident</span><span class="p">[</span><span class="mi">1</span><span class="p">]:</span>
                <span class="n">bottom_labels</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&#39;bottom_incident&#39;</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">bottom_labels</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&#39;bottom&#39;</span><span class="p">)</span>
        <span class="n">top_grain</span> <span class="o">=</span> <span class="n">Structure</span><span class="p">(</span><span class="n">Lattice</span><span class="p">(</span><span class="n">top_grain</span><span class="o">.</span><span class="n">lattice</span><span class="o">.</span><span class="n">matrix</span><span class="p">),</span> <span class="n">top_grain</span><span class="o">.</span><span class="n">species</span><span class="p">,</span>
                              <span class="n">top_grain</span><span class="o">.</span><span class="n">frac_coords</span><span class="p">,</span> <span class="n">site_properties</span><span class="o">=</span><span class="p">{</span><span class="s1">&#39;grain_label&#39;</span><span class="p">:</span> <span class="n">top_labels</span><span class="p">})</span>
        <span class="n">bottom_grain</span> <span class="o">=</span> <span class="n">Structure</span><span class="p">(</span><span class="n">Lattice</span><span class="p">(</span><span class="n">bottom_grain</span><span class="o">.</span><span class="n">lattice</span><span class="o">.</span><span class="n">matrix</span><span class="p">),</span> <span class="n">bottom_grain</span><span class="o">.</span><span class="n">species</span><span class="p">,</span>
                                 <span class="n">bottom_grain</span><span class="o">.</span><span class="n">frac_coords</span><span class="p">,</span> <span class="n">site_properties</span><span class="o">=</span><span class="p">{</span><span class="s1">&#39;grain_label&#39;</span><span class="p">:</span> <span class="n">bottom_labels</span><span class="p">})</span>

        <span class="c1"># expand both grains</span>
        <span class="n">top_grain</span><span class="o">.</span><span class="n">make_supercell</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">expand_times</span><span class="p">])</span>
        <span class="n">bottom_grain</span><span class="o">.</span><span class="n">make_supercell</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">expand_times</span><span class="p">])</span>
        <span class="n">top_grain</span> <span class="o">=</span> <span class="n">fix_pbc</span><span class="p">(</span><span class="n">top_grain</span><span class="p">)</span>
        <span class="n">bottom_grain</span> <span class="o">=</span> <span class="n">fix_pbc</span><span class="p">(</span><span class="n">bottom_grain</span><span class="p">)</span>

        <span class="c1"># determine the top-grain location.</span>
        <span class="n">edge_b</span> <span class="o">=</span> <span class="mf">1.0</span> <span class="o">-</span> <span class="nb">max</span><span class="p">(</span><span class="n">bottom_grain</span><span class="o">.</span><span class="n">frac_coords</span><span class="p">[:,</span> <span class="mi">2</span><span class="p">])</span>
        <span class="n">edge_t</span> <span class="o">=</span> <span class="mf">1.0</span> <span class="o">-</span> <span class="nb">max</span><span class="p">(</span><span class="n">top_grain</span><span class="o">.</span><span class="n">frac_coords</span><span class="p">[:,</span> <span class="mi">2</span><span class="p">])</span>
        <span class="n">c_adjust</span> <span class="o">=</span> <span class="p">(</span><span class="n">edge_t</span> <span class="o">-</span> <span class="n">edge_b</span><span class="p">)</span> <span class="o">/</span> <span class="mf">2.0</span>

        <span class="c1"># construct all species</span>
        <span class="n">all_species</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">all_species</span><span class="o">.</span><span class="n">extend</span><span class="p">([</span><span class="n">site</span><span class="o">.</span><span class="n">specie</span> <span class="k">for</span> <span class="n">site</span> <span class="ow">in</span> <span class="n">bottom_grain</span><span class="p">])</span>
        <span class="n">all_species</span><span class="o">.</span><span class="n">extend</span><span class="p">([</span><span class="n">site</span><span class="o">.</span><span class="n">specie</span> <span class="k">for</span> <span class="n">site</span> <span class="ow">in</span> <span class="n">top_grain</span><span class="p">])</span>

        <span class="n">half_lattice</span> <span class="o">=</span> <span class="n">top_grain</span><span class="o">.</span><span class="n">lattice</span>
        <span class="c1"># calculate translation vector, perpendicular to the plane</span>
        <span class="n">normal_v_plane</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">cross</span><span class="p">(</span><span class="n">half_lattice</span><span class="o">.</span><span class="n">matrix</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">half_lattice</span><span class="o">.</span><span class="n">matrix</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
        <span class="n">unit_normal_v</span> <span class="o">=</span> <span class="n">normal_v_plane</span> <span class="o">/</span> <span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">norm</span><span class="p">(</span><span class="n">normal_v_plane</span><span class="p">)</span>
        <span class="n">translation_v</span> <span class="o">=</span> <span class="n">unit_normal_v</span> <span class="o">*</span> <span class="n">vacuum_thickness</span>

        <span class="c1"># construct the final lattice</span>
        <span class="n">whole_matrix_no_vac</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">half_lattice</span><span class="o">.</span><span class="n">matrix</span><span class="p">)</span>
        <span class="n">whole_matrix_no_vac</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="n">half_lattice</span><span class="o">.</span><span class="n">matrix</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">*</span> <span class="mi">2</span>
        <span class="n">whole_matrix_with_vac</span> <span class="o">=</span> <span class="n">whole_matrix_no_vac</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
        <span class="n">whole_matrix_with_vac</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="n">whole_matrix_no_vac</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">+</span> <span class="n">translation_v</span> <span class="o">*</span> <span class="mi">2</span>
        <span class="n">whole_lat</span> <span class="o">=</span> <span class="n">Lattice</span><span class="p">(</span><span class="n">whole_matrix_with_vac</span><span class="p">)</span>

        <span class="c1"># construct the coords, move top grain with translation_v</span>
        <span class="n">all_coords</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">grain_labels</span> <span class="o">=</span> <span class="n">bottom_grain</span><span class="o">.</span><span class="n">site_properties</span><span class="p">[</span><span class="s1">&#39;grain_label&#39;</span><span class="p">]</span> <span class="o">+</span> <span class="n">top_grain</span><span class="o">.</span><span class="n">site_properties</span><span class="p">[</span><span class="s1">&#39;grain_label&#39;</span><span class="p">]</span>
        <span class="k">for</span> <span class="n">site</span> <span class="ow">in</span> <span class="n">bottom_grain</span><span class="p">:</span>
            <span class="n">all_coords</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">site</span><span class="o">.</span><span class="n">coords</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">site</span> <span class="ow">in</span> <span class="n">top_grain</span><span class="p">:</span>
            <span class="n">all_coords</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">site</span><span class="o">.</span><span class="n">coords</span> <span class="o">+</span> <span class="n">half_lattice</span><span class="o">.</span><span class="n">matrix</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">*</span> <span class="p">(</span><span class="mi">1</span> <span class="o">+</span> <span class="n">c_adjust</span><span class="p">)</span> <span class="o">+</span>
                              <span class="n">unit_ab_adjust</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">norm</span><span class="p">(</span><span class="n">half_lattice</span><span class="o">.</span><span class="n">matrix</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">*</span> <span class="p">(</span><span class="mi">1</span> <span class="o">+</span> <span class="n">c_adjust</span><span class="p">))</span> <span class="o">+</span>
                              <span class="n">translation_v</span> <span class="o">+</span> <span class="n">ab_shift</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">*</span> <span class="n">whole_matrix_with_vac</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span>
                              <span class="n">ab_shift</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">*</span> <span class="n">whole_matrix_with_vac</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>

        <span class="n">gb_with_vac</span> <span class="o">=</span> <span class="n">Structure</span><span class="p">(</span><span class="n">whole_lat</span><span class="p">,</span> <span class="n">all_species</span><span class="p">,</span> <span class="n">all_coords</span><span class="p">,</span>
                                <span class="n">coords_are_cartesian</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
                                <span class="n">site_properties</span><span class="o">=</span><span class="p">{</span><span class="s1">&#39;grain_label&#39;</span><span class="p">:</span> <span class="n">grain_labels</span><span class="p">})</span>
        <span class="c1"># merge closer atoms. extract near gb atoms.</span>
        <span class="n">cos_c_norm_plane</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">unit_normal_v</span><span class="p">,</span> <span class="n">whole_matrix_with_vac</span><span class="p">[</span><span class="mi">2</span><span class="p">])</span> <span class="o">/</span> <span class="n">whole_lat</span><span class="o">.</span><span class="n">c</span>
        <span class="n">range_c_len</span> <span class="o">=</span> <span class="nb">abs</span><span class="p">(</span><span class="n">bond_length</span> <span class="o">/</span> <span class="n">cos_c_norm_plane</span> <span class="o">/</span> <span class="n">whole_lat</span><span class="o">.</span><span class="n">c</span><span class="p">)</span>
        <span class="n">sites_near_gb</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">sites_away_gb</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">site</span> <span class="ow">in</span> <span class="n">gb_with_vac</span><span class="o">.</span><span class="n">sites</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">site</span><span class="o">.</span><span class="n">frac_coords</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">&lt;</span> <span class="n">range_c_len</span> <span class="ow">or</span> <span class="n">site</span><span class="o">.</span><span class="n">frac_coords</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">&gt;</span> <span class="mi">1</span> <span class="o">-</span> <span class="n">range_c_len</span> \
                    <span class="ow">or</span> <span class="p">(</span><span class="n">site</span><span class="o">.</span><span class="n">frac_coords</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">&gt;</span> <span class="mf">0.5</span> <span class="o">-</span> <span class="n">range_c_len</span> <span class="ow">and</span> <span class="n">site</span><span class="o">.</span><span class="n">frac_coords</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">&lt;</span> <span class="mf">0.5</span> <span class="o">+</span> <span class="n">range_c_len</span><span class="p">):</span>
                <span class="n">sites_near_gb</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">site</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">sites_away_gb</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">site</span><span class="p">)</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">sites_near_gb</span><span class="p">)</span> <span class="o">&gt;=</span> <span class="mi">1</span><span class="p">:</span>
            <span class="n">s_near_gb</span> <span class="o">=</span> <span class="n">Structure</span><span class="o">.</span><span class="n">from_sites</span><span class="p">(</span><span class="n">sites_near_gb</span><span class="p">)</span>
            <span class="n">s_near_gb</span><span class="o">.</span><span class="n">merge_sites</span><span class="p">(</span><span class="n">tol</span><span class="o">=</span><span class="n">bond_length</span> <span class="o">*</span> <span class="n">rm_ratio</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="s1">&#39;d&#39;</span><span class="p">)</span>
            <span class="n">all_sites</span> <span class="o">=</span> <span class="n">sites_away_gb</span> <span class="o">+</span> <span class="n">s_near_gb</span><span class="o">.</span><span class="n">sites</span>
            <span class="n">gb_with_vac</span> <span class="o">=</span> <span class="n">Structure</span><span class="o">.</span><span class="n">from_sites</span><span class="p">(</span><span class="n">all_sites</span><span class="p">)</span>

        <span class="c1"># move coordinates into the periodic cell.</span>
        <span class="n">gb_with_vac</span> <span class="o">=</span> <span class="n">fix_pbc</span><span class="p">(</span><span class="n">gb_with_vac</span><span class="p">,</span> <span class="n">whole_lat</span><span class="o">.</span><span class="n">matrix</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">GrainBoundary</span><span class="p">(</span><span class="n">whole_lat</span><span class="p">,</span> <span class="n">gb_with_vac</span><span class="o">.</span><span class="n">species</span><span class="p">,</span> <span class="n">gb_with_vac</span><span class="o">.</span><span class="n">cart_coords</span><span class="p">,</span> <span class="n">rotation_axis</span><span class="p">,</span>
                             <span class="n">rotation_angle</span><span class="p">,</span> <span class="n">plane</span><span class="p">,</span> <span class="n">join_plane</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">initial_structure</span><span class="p">,</span>
                             <span class="n">vacuum_thickness</span><span class="p">,</span> <span class="n">ab_shift</span><span class="p">,</span> <span class="n">site_properties</span><span class="o">=</span><span class="n">gb_with_vac</span><span class="o">.</span><span class="n">site_properties</span><span class="p">,</span>
                             <span class="n">oriented_unit_cell</span><span class="o">=</span><span class="n">oriended_unit_cell</span><span class="p">,</span>
                             <span class="n">coords_are_cartesian</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span></div>

<div class="viewcode-block" id="GrainBoundaryGenerator.get_ratio"><a class="viewcode-back" href="../../../../pymatgen.analysis.gb.grain.html#pymatgen.analysis.gb.grain.GrainBoundaryGenerator.get_ratio">[docs]</a>    <span class="k">def</span> <span class="nf">get_ratio</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">max_denominator</span><span class="o">=</span><span class="mi">5</span><span class="p">,</span> <span class="n">index_none</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        find the axial ratio needed for GB generator input.</span>
<span class="sd">        Args:</span>
<span class="sd">            max_denominator (int): the maximum denominator for</span>
<span class="sd">                the computed ratio, default to be 5.</span>
<span class="sd">            index_none (int): specify the irrational axis.</span>
<span class="sd">                0-a, 1-b, 2-c. Only may be needed for orthorhombic system.</span>
<span class="sd">        Returns:</span>
<span class="sd">               axial ratio needed for GB generator (list of integers).</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">structure</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">initial_structure</span>
        <span class="n">lat_type</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">lat_type</span>
        <span class="k">if</span> <span class="n">lat_type</span> <span class="o">==</span> <span class="s1">&#39;t&#39;</span> <span class="ow">or</span> <span class="n">lat_type</span> <span class="o">==</span> <span class="s1">&#39;h&#39;</span><span class="p">:</span>
            <span class="c1"># For tetragonal and hexagonal system, ratio = c2 / a2.</span>
            <span class="n">a</span><span class="p">,</span> <span class="n">c</span> <span class="o">=</span> <span class="p">(</span><span class="n">structure</span><span class="o">.</span><span class="n">lattice</span><span class="o">.</span><span class="n">a</span><span class="p">,</span> <span class="n">structure</span><span class="o">.</span><span class="n">lattice</span><span class="o">.</span><span class="n">c</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">c</span> <span class="o">&gt;</span> <span class="n">a</span><span class="p">:</span>
                <span class="n">frac</span> <span class="o">=</span> <span class="n">Fraction</span><span class="p">(</span><span class="n">c</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">/</span> <span class="n">a</span> <span class="o">**</span> <span class="mi">2</span><span class="p">)</span><span class="o">.</span><span class="n">limit_denominator</span><span class="p">(</span><span class="n">max_denominator</span><span class="p">)</span>
                <span class="n">ratio</span> <span class="o">=</span> <span class="p">[</span><span class="n">frac</span><span class="o">.</span><span class="n">numerator</span><span class="p">,</span> <span class="n">frac</span><span class="o">.</span><span class="n">denominator</span><span class="p">]</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">frac</span> <span class="o">=</span> <span class="n">Fraction</span><span class="p">(</span><span class="n">a</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">/</span> <span class="n">c</span> <span class="o">**</span> <span class="mi">2</span><span class="p">)</span><span class="o">.</span><span class="n">limit_denominator</span><span class="p">(</span><span class="n">max_denominator</span><span class="p">)</span>
                <span class="n">ratio</span> <span class="o">=</span> <span class="p">[</span><span class="n">frac</span><span class="o">.</span><span class="n">denominator</span><span class="p">,</span> <span class="n">frac</span><span class="o">.</span><span class="n">numerator</span><span class="p">]</span>
        <span class="k">elif</span> <span class="n">lat_type</span> <span class="o">==</span> <span class="s1">&#39;r&#39;</span><span class="p">:</span>
            <span class="c1"># For rhombohedral system, ratio = (1 + 2 * cos(alpha)) / cos(alpha).</span>
            <span class="n">cos_alpha</span> <span class="o">=</span> <span class="n">cos</span><span class="p">(</span><span class="n">structure</span><span class="o">.</span><span class="n">lattice</span><span class="o">.</span><span class="n">alpha</span> <span class="o">/</span> <span class="mi">180</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">pi</span><span class="p">)</span>
            <span class="n">frac</span> <span class="o">=</span> <span class="n">Fraction</span><span class="p">((</span><span class="mi">1</span> <span class="o">+</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">cos_alpha</span><span class="p">)</span> <span class="o">/</span> <span class="n">cos_alpha</span><span class="p">)</span><span class="o">.</span><span class="n">limit_denominator</span><span class="p">(</span><span class="n">max_denominator</span><span class="p">)</span>
            <span class="n">ratio</span> <span class="o">=</span> <span class="p">[</span><span class="n">frac</span><span class="o">.</span><span class="n">numerator</span><span class="p">,</span> <span class="n">frac</span><span class="o">.</span><span class="n">denominator</span><span class="p">]</span>
        <span class="k">elif</span> <span class="n">lat_type</span> <span class="o">==</span> <span class="s1">&#39;o&#39;</span><span class="p">:</span>
            <span class="c1"># For orthorhombic system, ratio = c2:b2:a2.If irrational for one axis, set it to None.</span>
            <span class="n">ratio</span> <span class="o">=</span> <span class="p">[</span><span class="kc">None</span><span class="p">]</span> <span class="o">*</span> <span class="mi">3</span>
            <span class="n">lat</span> <span class="o">=</span> <span class="p">(</span><span class="n">structure</span><span class="o">.</span><span class="n">lattice</span><span class="o">.</span><span class="n">c</span><span class="p">,</span> <span class="n">structure</span><span class="o">.</span><span class="n">lattice</span><span class="o">.</span><span class="n">b</span><span class="p">,</span> <span class="n">structure</span><span class="o">.</span><span class="n">lattice</span><span class="o">.</span><span class="n">a</span><span class="p">)</span>
            <span class="n">index</span> <span class="o">=</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">]</span>
            <span class="k">if</span> <span class="n">index_none</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">min_index</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">argmin</span><span class="p">(</span><span class="n">lat</span><span class="p">)</span>
                <span class="n">index</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="n">min_index</span><span class="p">)</span>
                <span class="n">frac1</span> <span class="o">=</span> <span class="n">Fraction</span><span class="p">(</span><span class="n">lat</span><span class="p">[</span><span class="n">index</span><span class="p">[</span><span class="mi">0</span><span class="p">]]</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">/</span> <span class="n">lat</span><span class="p">[</span><span class="n">min_index</span><span class="p">]</span> <span class="o">**</span> <span class="mi">2</span><span class="p">)</span><span class="o">.</span><span class="n">limit_denominator</span><span class="p">(</span><span class="n">max_denominator</span><span class="p">)</span>
                <span class="n">frac2</span> <span class="o">=</span> <span class="n">Fraction</span><span class="p">(</span><span class="n">lat</span><span class="p">[</span><span class="n">index</span><span class="p">[</span><span class="mi">1</span><span class="p">]]</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">/</span> <span class="n">lat</span><span class="p">[</span><span class="n">min_index</span><span class="p">]</span> <span class="o">**</span> <span class="mi">2</span><span class="p">)</span><span class="o">.</span><span class="n">limit_denominator</span><span class="p">(</span><span class="n">max_denominator</span><span class="p">)</span>
                <span class="n">com_lcm</span> <span class="o">=</span> <span class="n">lcm</span><span class="p">(</span><span class="n">frac1</span><span class="o">.</span><span class="n">denominator</span><span class="p">,</span> <span class="n">frac2</span><span class="o">.</span><span class="n">denominator</span><span class="p">)</span>
                <span class="n">ratio</span><span class="p">[</span><span class="n">min_index</span><span class="p">]</span> <span class="o">=</span> <span class="n">com_lcm</span>
                <span class="n">ratio</span><span class="p">[</span><span class="n">index</span><span class="p">[</span><span class="mi">0</span><span class="p">]]</span> <span class="o">=</span> <span class="n">frac1</span><span class="o">.</span><span class="n">numerator</span> <span class="o">*</span> <span class="nb">int</span><span class="p">(</span><span class="nb">round</span><span class="p">((</span><span class="n">com_lcm</span> <span class="o">/</span> <span class="n">frac1</span><span class="o">.</span><span class="n">denominator</span><span class="p">)))</span>
                <span class="n">ratio</span><span class="p">[</span><span class="n">index</span><span class="p">[</span><span class="mi">1</span><span class="p">]]</span> <span class="o">=</span> <span class="n">frac2</span><span class="o">.</span><span class="n">numerator</span> <span class="o">*</span> <span class="nb">int</span><span class="p">(</span><span class="nb">round</span><span class="p">((</span><span class="n">com_lcm</span> <span class="o">/</span> <span class="n">frac2</span><span class="o">.</span><span class="n">denominator</span><span class="p">)))</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">index</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="n">index_none</span><span class="p">)</span>
                <span class="k">if</span> <span class="p">(</span><span class="n">lat</span><span class="p">[</span><span class="n">index</span><span class="p">[</span><span class="mi">0</span><span class="p">]]</span> <span class="o">&gt;</span> <span class="n">lat</span><span class="p">[</span><span class="n">index</span><span class="p">[</span><span class="mi">1</span><span class="p">]]):</span>
                    <span class="n">frac</span> <span class="o">=</span> <span class="n">Fraction</span><span class="p">(</span><span class="n">lat</span><span class="p">[</span><span class="n">index</span><span class="p">[</span><span class="mi">0</span><span class="p">]]</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">/</span> <span class="n">lat</span><span class="p">[</span><span class="n">index</span><span class="p">[</span><span class="mi">1</span><span class="p">]]</span> <span class="o">**</span> <span class="mi">2</span><span class="p">)</span><span class="o">.</span><span class="n">limit_denominator</span><span class="p">(</span><span class="n">max_denominator</span><span class="p">)</span>
                    <span class="n">ratio</span><span class="p">[</span><span class="n">index</span><span class="p">[</span><span class="mi">0</span><span class="p">]]</span> <span class="o">=</span> <span class="n">frac</span><span class="o">.</span><span class="n">numerator</span>
                    <span class="n">ratio</span><span class="p">[</span><span class="n">index</span><span class="p">[</span><span class="mi">1</span><span class="p">]]</span> <span class="o">=</span> <span class="n">frac</span><span class="o">.</span><span class="n">denominator</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">frac</span> <span class="o">=</span> <span class="n">Fraction</span><span class="p">(</span><span class="n">lat</span><span class="p">[</span><span class="n">index</span><span class="p">[</span><span class="mi">1</span><span class="p">]]</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">/</span> <span class="n">lat</span><span class="p">[</span><span class="n">index</span><span class="p">[</span><span class="mi">0</span><span class="p">]]</span> <span class="o">**</span> <span class="mi">2</span><span class="p">)</span><span class="o">.</span><span class="n">limit_denominator</span><span class="p">(</span><span class="n">max_denominator</span><span class="p">)</span>
                    <span class="n">ratio</span><span class="p">[</span><span class="n">index</span><span class="p">[</span><span class="mi">1</span><span class="p">]]</span> <span class="o">=</span> <span class="n">frac</span><span class="o">.</span><span class="n">numerator</span>
                    <span class="n">ratio</span><span class="p">[</span><span class="n">index</span><span class="p">[</span><span class="mi">0</span><span class="p">]]</span> <span class="o">=</span> <span class="n">frac</span><span class="o">.</span><span class="n">denominator</span>
        <span class="k">elif</span> <span class="n">lat_type</span> <span class="o">==</span> <span class="s1">&#39;c&#39;</span><span class="p">:</span>
            <span class="c1"># Cubic system does not need axial ratio.</span>
            <span class="k">return</span> <span class="kc">None</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s1">&#39;Lattice type not implemented.&#39;</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">ratio</span></div>

<div class="viewcode-block" id="GrainBoundaryGenerator.get_trans_mat"><a class="viewcode-back" href="../../../../pymatgen.analysis.gb.grain.html#pymatgen.analysis.gb.grain.GrainBoundaryGenerator.get_trans_mat">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">get_trans_mat</span><span class="p">(</span><span class="n">r_axis</span><span class="p">,</span> <span class="n">angle</span><span class="p">,</span> <span class="n">normal</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">trans_cry</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">eye</span><span class="p">(</span><span class="mi">3</span><span class="p">),</span> <span class="n">lat_type</span><span class="o">=</span><span class="s1">&#39;c&#39;</span><span class="p">,</span>
                      <span class="n">ratio</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">surface</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">max_search</span><span class="o">=</span><span class="mi">20</span><span class="p">,</span> <span class="n">quick_gen</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Find the two transformation matrix for each grain from given rotation axis,</span>
<span class="sd">        GB plane, rotation angle and corresponding ratio (see explanation for ratio</span>
<span class="sd">        below).</span>
<span class="sd">        The structure of each grain can be obtained by applying the corresponding</span>
<span class="sd">        transformation matrix to the conventional cell.</span>
<span class="sd">        The algorithm for this code is from reference, Acta Cryst, A32,783(1976).</span>

<span class="sd">        Args:</span>
<span class="sd">            r_axis (list of three integers, e.g. u, v, w</span>
<span class="sd">                    or four integers, e.g. u, v, t, w for hex/rho system only):</span>
<span class="sd">                    the rotation axis of the grain boundary.</span>
<span class="sd">            angle (float, in unit of degree) :</span>
<span class="sd">                    the rotation angle of the grain boundary</span>
<span class="sd">            normal (logic):</span>
<span class="sd">                    determine if need to require the c axis of one grain associated with</span>
<span class="sd">                    the first transformation matrix perperdicular to the surface or not.</span>
<span class="sd">                    default to false.</span>
<span class="sd">            trans_cry (3 by 3 array):</span>
<span class="sd">                    if the structure given are primitive cell in cubic system, e.g.</span>
<span class="sd">                    bcc or fcc system, trans_cry is the transformation matrix from its</span>
<span class="sd">                    conventional cell to the primitive cell.</span>
<span class="sd">            lat_type ( one character):</span>
<span class="sd">                    &#39;c&#39; or &#39;C&#39;: cubic system</span>
<span class="sd">                     &#39;t&#39; or &#39;T&#39;: tetragonal system</span>
<span class="sd">                     &#39;o&#39; or &#39;O&#39;: orthorhombic system</span>
<span class="sd">                     &#39;h&#39; or &#39;H&#39;: hexagonal system</span>
<span class="sd">                     &#39;r&#39; or &#39;R&#39;: rhombohedral system</span>
<span class="sd">                     default to cubic system</span>
<span class="sd">            ratio (list of integers):</span>
<span class="sd">                    lattice axial ratio.</span>
<span class="sd">                    For cubic system, ratio is not needed.</span>
<span class="sd">                    For tetragonal system, ratio = [mu, mv], list of two integers,</span>
<span class="sd">                    that is, mu/mv = c2/a2. If it is irrational, set it to none.</span>
<span class="sd">                    For orthorhombic system, ratio = [mu, lam, mv], list of three integers,</span>
<span class="sd">                    that is, mu:lam:mv = c2:b2:a2. If irrational for one axis, set it to None.</span>
<span class="sd">                    e.g. mu:lam:mv = c2,None,a2, means b2 is irrational.</span>
<span class="sd">                    For rhombohedral system, ratio = [mu, mv], list of two integers,</span>
<span class="sd">                    that is, mu/mv is the ratio of (1+2*cos(alpha)/cos(alpha).</span>
<span class="sd">                    If irrational, set it to None.</span>
<span class="sd">                    For hexagonal system, ratio = [mu, mv], list of two integers,</span>
<span class="sd">                    that is, mu/mv = c2/a2. If it is irrational, set it to none.</span>
<span class="sd">            surface (list of three integers, e.g. h, k, l</span>
<span class="sd">                     or four integers, e.g. h, k, i, l for hex/rho system only):</span>
<span class="sd">                    the miller index of grain boundary plane, with the format of [h,k,l]</span>
<span class="sd">                    if surface is not given, the default is perpendicular to r_axis, which is</span>
<span class="sd">                    a twist grain boundary.</span>
<span class="sd">            max_search (int): max search for the GB lattice vectors that give the smallest GB</span>
<span class="sd">                lattice. If normal is true, also max search the GB c vector that perpendicular</span>
<span class="sd">                to the plane.</span>
<span class="sd">            quick_gen (bool): whether to quickly generate a supercell, if set to true, no need to</span>
<span class="sd">                find the smallest cell.</span>
<span class="sd">        Returns:</span>
<span class="sd">            t1 (3 by 3 integer array):</span>
<span class="sd">                    The transformation array for one grain.</span>
<span class="sd">            t2 (3 by 3 integer array):</span>
<span class="sd">                    The transformation array for the other grain</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># transform four index notation to three index notation</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">r_axis</span><span class="p">)</span> <span class="o">==</span> <span class="mi">4</span><span class="p">:</span>
            <span class="n">u1</span> <span class="o">=</span> <span class="n">r_axis</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
            <span class="n">v1</span> <span class="o">=</span> <span class="n">r_axis</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
            <span class="n">w1</span> <span class="o">=</span> <span class="n">r_axis</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span>
            <span class="k">if</span> <span class="n">lat_type</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="o">==</span> <span class="s1">&#39;h&#39;</span><span class="p">:</span>
                <span class="n">u</span> <span class="o">=</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">u1</span> <span class="o">+</span> <span class="n">v1</span>
                <span class="n">v</span> <span class="o">=</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">v1</span> <span class="o">+</span> <span class="n">u1</span>
                <span class="n">w</span> <span class="o">=</span> <span class="n">w1</span>
                <span class="n">r_axis</span> <span class="o">=</span> <span class="p">[</span><span class="n">u</span><span class="p">,</span> <span class="n">v</span><span class="p">,</span> <span class="n">w</span><span class="p">]</span>
            <span class="k">elif</span> <span class="n">lat_type</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="o">==</span> <span class="s1">&#39;r&#39;</span><span class="p">:</span>
                <span class="n">u</span> <span class="o">=</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">u1</span> <span class="o">+</span> <span class="n">v1</span> <span class="o">+</span> <span class="n">w1</span>
                <span class="n">v</span> <span class="o">=</span> <span class="n">v1</span> <span class="o">+</span> <span class="n">w1</span> <span class="o">-</span> <span class="n">u1</span>
                <span class="n">w</span> <span class="o">=</span> <span class="n">w1</span> <span class="o">-</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">v1</span> <span class="o">-</span> <span class="n">u1</span>
                <span class="n">r_axis</span> <span class="o">=</span> <span class="p">[</span><span class="n">u</span><span class="p">,</span> <span class="n">v</span><span class="p">,</span> <span class="n">w</span><span class="p">]</span>

        <span class="c1"># make sure gcd(r_axis)==1</span>
        <span class="k">if</span> <span class="n">reduce</span><span class="p">(</span><span class="n">gcd</span><span class="p">,</span> <span class="n">r_axis</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">1</span><span class="p">:</span>
            <span class="n">r_axis</span> <span class="o">=</span> <span class="p">[</span><span class="nb">int</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">x</span> <span class="o">/</span> <span class="n">reduce</span><span class="p">(</span><span class="n">gcd</span><span class="p">,</span> <span class="n">r_axis</span><span class="p">)))</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">r_axis</span><span class="p">]</span>

        <span class="k">if</span> <span class="n">surface</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">surface</span><span class="p">)</span> <span class="o">==</span> <span class="mi">4</span><span class="p">:</span>
                <span class="n">u1</span> <span class="o">=</span> <span class="n">surface</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
                <span class="n">v1</span> <span class="o">=</span> <span class="n">surface</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
                <span class="n">w1</span> <span class="o">=</span> <span class="n">surface</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span>
                <span class="n">surface</span> <span class="o">=</span> <span class="p">[</span><span class="n">u1</span><span class="p">,</span> <span class="n">v1</span><span class="p">,</span> <span class="n">w1</span><span class="p">]</span>
        <span class="c1"># set the surface for grain boundary.</span>
        <span class="k">if</span> <span class="n">surface</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">lat_type</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="o">==</span> <span class="s1">&#39;c&#39;</span><span class="p">:</span>
                <span class="n">surface</span> <span class="o">=</span> <span class="n">r_axis</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">lat_type</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="o">==</span> <span class="s1">&#39;h&#39;</span><span class="p">:</span>
                    <span class="k">if</span> <span class="n">ratio</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                        <span class="n">c2_a2_ratio</span> <span class="o">=</span> <span class="mi">1</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="n">c2_a2_ratio</span> <span class="o">=</span> <span class="n">ratio</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">/</span> <span class="n">ratio</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
                    <span class="n">metric</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([[</span><span class="mi">1</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.5</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="o">-</span><span class="mf">0.5</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">c2_a2_ratio</span><span class="p">]])</span>
                <span class="k">elif</span> <span class="n">lat_type</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="o">==</span> <span class="s1">&#39;r&#39;</span><span class="p">:</span>
                    <span class="k">if</span> <span class="n">ratio</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                        <span class="n">cos_alpha</span> <span class="o">=</span> <span class="mf">0.5</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="n">cos_alpha</span> <span class="o">=</span> <span class="mf">1.0</span> <span class="o">/</span> <span class="p">(</span><span class="n">ratio</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">/</span> <span class="n">ratio</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">-</span> <span class="mi">2</span><span class="p">)</span>
                    <span class="n">metric</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([[</span><span class="mi">1</span><span class="p">,</span> <span class="n">cos_alpha</span><span class="p">,</span> <span class="n">cos_alpha</span><span class="p">],</span> <span class="p">[</span><span class="n">cos_alpha</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">cos_alpha</span><span class="p">],</span>
                                       <span class="p">[</span><span class="n">cos_alpha</span><span class="p">,</span> <span class="n">cos_alpha</span><span class="p">,</span> <span class="mi">1</span><span class="p">]])</span>
                <span class="k">elif</span> <span class="n">lat_type</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="o">==</span> <span class="s1">&#39;t&#39;</span><span class="p">:</span>
                    <span class="k">if</span> <span class="n">ratio</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                        <span class="n">c2_a2_ratio</span> <span class="o">=</span> <span class="mi">1</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="n">c2_a2_ratio</span> <span class="o">=</span> <span class="n">ratio</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">/</span> <span class="n">ratio</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
                    <span class="n">metric</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">c2_a2_ratio</span><span class="p">]])</span>
                <span class="k">elif</span> <span class="n">lat_type</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="o">==</span> <span class="s1">&#39;o&#39;</span><span class="p">:</span>
                    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">3</span><span class="p">):</span>
                        <span class="k">if</span> <span class="n">ratio</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                            <span class="n">ratio</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span>
                    <span class="n">metric</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="n">ratio</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">/</span> <span class="n">ratio</span><span class="p">[</span><span class="mi">2</span><span class="p">],</span> <span class="mi">0</span><span class="p">],</span>
                                       <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">ratio</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">/</span> <span class="n">ratio</span><span class="p">[</span><span class="mi">2</span><span class="p">]]])</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s1">&#39;Lattice type has not implemented.&#39;</span><span class="p">)</span>

                <span class="n">surface</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">matmul</span><span class="p">(</span><span class="n">r_axis</span><span class="p">,</span> <span class="n">metric</span><span class="p">)</span>
                <span class="n">fractions</span> <span class="o">=</span> <span class="p">[</span><span class="n">Fraction</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">.</span><span class="n">limit_denominator</span><span class="p">()</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">surface</span><span class="p">]</span>
                <span class="n">least_mul</span> <span class="o">=</span> <span class="n">reduce</span><span class="p">(</span><span class="n">lcm</span><span class="p">,</span> <span class="p">[</span><span class="n">f</span><span class="o">.</span><span class="n">denominator</span> <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">fractions</span><span class="p">])</span>
                <span class="n">surface</span> <span class="o">=</span> <span class="p">[</span><span class="nb">int</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">x</span> <span class="o">*</span> <span class="n">least_mul</span><span class="p">))</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">surface</span><span class="p">]</span>

        <span class="k">if</span> <span class="n">reduce</span><span class="p">(</span><span class="n">gcd</span><span class="p">,</span> <span class="n">surface</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">1</span><span class="p">:</span>
            <span class="n">index</span> <span class="o">=</span> <span class="n">reduce</span><span class="p">(</span><span class="n">gcd</span><span class="p">,</span> <span class="n">surface</span><span class="p">)</span>
            <span class="n">surface</span> <span class="o">=</span> <span class="p">[</span><span class="nb">int</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">x</span> <span class="o">/</span> <span class="n">index</span><span class="p">))</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">surface</span><span class="p">]</span>

        <span class="k">if</span> <span class="n">lat_type</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="o">==</span> <span class="s1">&#39;h&#39;</span><span class="p">:</span>
            <span class="c1"># set the value for u,v,w,mu,mv,m,n,d,x</span>
            <span class="c1"># check the reference for the meaning of these parameters</span>
            <span class="n">u</span><span class="p">,</span> <span class="n">v</span><span class="p">,</span> <span class="n">w</span> <span class="o">=</span> <span class="n">r_axis</span>
            <span class="c1"># make sure mu, mv are coprime integers.</span>
            <span class="k">if</span> <span class="n">ratio</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">mu</span><span class="p">,</span> <span class="n">mv</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">]</span>
                <span class="k">if</span> <span class="n">w</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">:</span>
                    <span class="k">if</span> <span class="n">u</span> <span class="o">!=</span> <span class="mi">0</span> <span class="ow">or</span> <span class="p">(</span><span class="n">v</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">):</span>
                        <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s1">&#39;For irrational c2/a2, CSL only exist for [0,0,1] &#39;</span>
                                           <span class="s1">&#39;or [u,v,0] and m = 0&#39;</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">mu</span><span class="p">,</span> <span class="n">mv</span> <span class="o">=</span> <span class="n">ratio</span>
            <span class="k">if</span> <span class="n">gcd</span><span class="p">(</span><span class="n">mu</span><span class="p">,</span> <span class="n">mv</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">1</span><span class="p">:</span>
                <span class="n">temp</span> <span class="o">=</span> <span class="n">gcd</span><span class="p">(</span><span class="n">mu</span><span class="p">,</span> <span class="n">mv</span><span class="p">)</span>
                <span class="n">mu</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">mu</span> <span class="o">/</span> <span class="n">temp</span><span class="p">))</span>
                <span class="n">mv</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">mv</span> <span class="o">/</span> <span class="n">temp</span><span class="p">))</span>
            <span class="n">d</span> <span class="o">=</span> <span class="p">(</span><span class="n">u</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span> <span class="n">v</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">-</span> <span class="n">u</span> <span class="o">*</span> <span class="n">v</span><span class="p">)</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">+</span> <span class="n">w</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mu</span>
            <span class="k">if</span> <span class="nb">abs</span><span class="p">(</span><span class="n">angle</span> <span class="o">-</span> <span class="mf">180.0</span><span class="p">)</span> <span class="o">&lt;</span> <span class="mf">1.e0</span><span class="p">:</span>
                <span class="n">m</span> <span class="o">=</span> <span class="mi">0</span>
                <span class="n">n</span> <span class="o">=</span> <span class="mi">1</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">fraction</span> <span class="o">=</span> <span class="n">Fraction</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">tan</span><span class="p">(</span><span class="n">angle</span> <span class="o">/</span> <span class="mi">2</span> <span class="o">/</span> <span class="mf">180.0</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">pi</span><span class="p">)</span> <span class="o">/</span>
                                    <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="n">d</span><span class="p">)</span> <span class="o">/</span> <span class="mf">3.0</span> <span class="o">/</span> <span class="n">mu</span><span class="p">))</span><span class="o">.</span><span class="n">limit_denominator</span><span class="p">()</span>
                <span class="n">m</span> <span class="o">=</span> <span class="n">fraction</span><span class="o">.</span><span class="n">denominator</span>
                <span class="n">n</span> <span class="o">=</span> <span class="n">fraction</span><span class="o">.</span><span class="n">numerator</span>

            <span class="c1"># construct the rotation matrix, check reference for details</span>
            <span class="n">r_list</span> <span class="o">=</span> <span class="p">[(</span><span class="n">u</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">-</span> <span class="n">v</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">-</span> <span class="n">w</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mu</span><span class="p">)</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span>
                      <span class="mi">2</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="mi">3</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">**</span> <span class="mi">2</span><span class="p">,</span>
                      <span class="p">(</span><span class="mi">2</span> <span class="o">*</span> <span class="n">v</span> <span class="o">-</span> <span class="n">u</span><span class="p">)</span> <span class="o">*</span> <span class="n">u</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">-</span> <span class="mi">4</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span><span class="p">,</span>
                      <span class="mi">2</span> <span class="o">*</span> <span class="n">u</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span> <span class="mi">2</span> <span class="o">*</span> <span class="p">(</span><span class="mi">2</span> <span class="o">*</span> <span class="n">v</span> <span class="o">-</span> <span class="n">u</span><span class="p">)</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span><span class="p">,</span>
                      <span class="p">(</span><span class="mi">2</span> <span class="o">*</span> <span class="n">u</span> <span class="o">-</span> <span class="n">v</span><span class="p">)</span> <span class="o">*</span> <span class="n">v</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span> <span class="mi">4</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span><span class="p">,</span>
                      <span class="p">(</span><span class="n">v</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">-</span> <span class="n">u</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">-</span> <span class="n">w</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mu</span><span class="p">)</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">-</span>
                      <span class="mi">2</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="mi">3</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">**</span> <span class="mi">2</span><span class="p">,</span>
                      <span class="mi">2</span> <span class="o">*</span> <span class="n">v</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">-</span> <span class="mi">2</span> <span class="o">*</span> <span class="p">(</span><span class="mi">2</span> <span class="o">*</span> <span class="n">u</span> <span class="o">-</span> <span class="n">v</span><span class="p">)</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span><span class="p">,</span>
                      <span class="p">(</span><span class="mi">2</span> <span class="o">*</span> <span class="n">u</span> <span class="o">-</span> <span class="n">v</span><span class="p">)</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">-</span> <span class="mi">3</span> <span class="o">*</span> <span class="n">v</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span><span class="p">,</span>
                      <span class="p">(</span><span class="mi">2</span> <span class="o">*</span> <span class="n">v</span> <span class="o">-</span> <span class="n">u</span><span class="p">)</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span> <span class="mi">3</span> <span class="o">*</span> <span class="n">u</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span><span class="p">,</span>
                      <span class="p">(</span><span class="n">w</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">-</span> <span class="n">u</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">-</span> <span class="n">v</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">+</span> <span class="n">u</span> <span class="o">*</span> <span class="n">v</span> <span class="o">*</span> <span class="n">mv</span><span class="p">)</span> <span class="o">*</span>
                      <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span> <span class="mi">3</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">**</span> <span class="mi">2</span><span class="p">]</span>
            <span class="n">m</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span> <span class="o">*</span> <span class="n">m</span>
            <span class="n">r_list_inv</span> <span class="o">=</span> <span class="p">[(</span><span class="n">u</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">-</span> <span class="n">v</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">-</span> <span class="n">w</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mu</span><span class="p">)</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span>
                          <span class="mi">2</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="mi">3</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">**</span> <span class="mi">2</span><span class="p">,</span>
                          <span class="p">(</span><span class="mi">2</span> <span class="o">*</span> <span class="n">v</span> <span class="o">-</span> <span class="n">u</span><span class="p">)</span> <span class="o">*</span> <span class="n">u</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">-</span> <span class="mi">4</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span><span class="p">,</span>
                          <span class="mi">2</span> <span class="o">*</span> <span class="n">u</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span> <span class="mi">2</span> <span class="o">*</span> <span class="p">(</span><span class="mi">2</span> <span class="o">*</span> <span class="n">v</span> <span class="o">-</span> <span class="n">u</span><span class="p">)</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span><span class="p">,</span>
                          <span class="p">(</span><span class="mi">2</span> <span class="o">*</span> <span class="n">u</span> <span class="o">-</span> <span class="n">v</span><span class="p">)</span> <span class="o">*</span> <span class="n">v</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span> <span class="mi">4</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span><span class="p">,</span>
                          <span class="p">(</span><span class="n">v</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">-</span> <span class="n">u</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">-</span> <span class="n">w</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mu</span><span class="p">)</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">-</span>
                          <span class="mi">2</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="mi">3</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">**</span> <span class="mi">2</span><span class="p">,</span>
                          <span class="mi">2</span> <span class="o">*</span> <span class="n">v</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">-</span> <span class="mi">2</span> <span class="o">*</span> <span class="p">(</span><span class="mi">2</span> <span class="o">*</span> <span class="n">u</span> <span class="o">-</span> <span class="n">v</span><span class="p">)</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span><span class="p">,</span>
                          <span class="p">(</span><span class="mi">2</span> <span class="o">*</span> <span class="n">u</span> <span class="o">-</span> <span class="n">v</span><span class="p">)</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">-</span> <span class="mi">3</span> <span class="o">*</span> <span class="n">v</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span><span class="p">,</span>
                          <span class="p">(</span><span class="mi">2</span> <span class="o">*</span> <span class="n">v</span> <span class="o">-</span> <span class="n">u</span><span class="p">)</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span> <span class="mi">3</span> <span class="o">*</span> <span class="n">u</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span><span class="p">,</span>
                          <span class="p">(</span><span class="n">w</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">-</span> <span class="n">u</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">-</span> <span class="n">v</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">+</span> <span class="n">u</span> <span class="o">*</span> <span class="n">v</span> <span class="o">*</span> <span class="n">mv</span><span class="p">)</span> <span class="o">*</span>
                          <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span> <span class="mi">3</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">**</span> <span class="mi">2</span><span class="p">]</span>
            <span class="n">m</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span> <span class="o">*</span> <span class="n">m</span>
            <span class="n">F</span> <span class="o">=</span> <span class="mi">3</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span> <span class="n">d</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span>
            <span class="n">all_list</span> <span class="o">=</span> <span class="n">r_list</span> <span class="o">+</span> <span class="n">r_list_inv</span> <span class="o">+</span> <span class="p">[</span><span class="n">F</span><span class="p">]</span>
            <span class="n">com_fac</span> <span class="o">=</span> <span class="n">reduce</span><span class="p">(</span><span class="n">gcd</span><span class="p">,</span> <span class="n">all_list</span><span class="p">)</span>
            <span class="n">sigma</span> <span class="o">=</span> <span class="n">F</span> <span class="o">/</span> <span class="n">com_fac</span>
            <span class="n">r_matrix</span> <span class="o">=</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">r_list</span><span class="p">)</span> <span class="o">/</span> <span class="n">com_fac</span> <span class="o">/</span> <span class="n">sigma</span><span class="p">)</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">lat_type</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="o">==</span> <span class="s1">&#39;r&#39;</span><span class="p">:</span>
            <span class="c1"># set the value for u,v,w,mu,mv,m,n,d</span>
            <span class="c1"># check the reference for the meaning of these parameters</span>
            <span class="n">u</span><span class="p">,</span> <span class="n">v</span><span class="p">,</span> <span class="n">w</span> <span class="o">=</span> <span class="n">r_axis</span>
            <span class="c1"># make sure mu, mv are coprime integers.</span>
            <span class="k">if</span> <span class="n">ratio</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">mu</span><span class="p">,</span> <span class="n">mv</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">]</span>
                <span class="k">if</span> <span class="n">u</span> <span class="o">+</span> <span class="n">v</span> <span class="o">+</span> <span class="n">w</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">:</span>
                    <span class="k">if</span> <span class="n">u</span> <span class="o">!=</span> <span class="n">v</span> <span class="ow">or</span> <span class="n">u</span> <span class="o">!=</span> <span class="n">w</span><span class="p">:</span>
                        <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s1">&#39;For irrational ratio_alpha, CSL only exist for [1,1,1]&#39;</span>
                                           <span class="s1">&#39;or [u, v, -(u+v)] and m =0&#39;</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">mu</span><span class="p">,</span> <span class="n">mv</span> <span class="o">=</span> <span class="n">ratio</span>
            <span class="k">if</span> <span class="n">gcd</span><span class="p">(</span><span class="n">mu</span><span class="p">,</span> <span class="n">mv</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">1</span><span class="p">:</span>
                <span class="n">temp</span> <span class="o">=</span> <span class="n">gcd</span><span class="p">(</span><span class="n">mu</span><span class="p">,</span> <span class="n">mv</span><span class="p">)</span>
                <span class="n">mu</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">mu</span> <span class="o">/</span> <span class="n">temp</span><span class="p">))</span>
                <span class="n">mv</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">mv</span> <span class="o">/</span> <span class="n">temp</span><span class="p">))</span>
            <span class="n">d</span> <span class="o">=</span> <span class="p">(</span><span class="n">u</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span> <span class="n">v</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span> <span class="n">w</span> <span class="o">**</span> <span class="mi">2</span><span class="p">)</span> <span class="o">*</span> <span class="p">(</span><span class="n">mu</span> <span class="o">-</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span><span class="p">)</span> <span class="o">+</span> \
                <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="p">(</span><span class="n">v</span> <span class="o">*</span> <span class="n">w</span> <span class="o">+</span> <span class="n">w</span> <span class="o">*</span> <span class="n">u</span> <span class="o">+</span> <span class="n">u</span> <span class="o">*</span> <span class="n">v</span><span class="p">)</span>
            <span class="k">if</span> <span class="nb">abs</span><span class="p">(</span><span class="n">angle</span> <span class="o">-</span> <span class="mf">180.0</span><span class="p">)</span> <span class="o">&lt;</span> <span class="mf">1.e0</span><span class="p">:</span>
                <span class="n">m</span> <span class="o">=</span> <span class="mi">0</span>
                <span class="n">n</span> <span class="o">=</span> <span class="mi">1</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">fraction</span> <span class="o">=</span> <span class="n">Fraction</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">tan</span><span class="p">(</span><span class="n">angle</span> <span class="o">/</span> <span class="mi">2</span> <span class="o">/</span> <span class="mf">180.0</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">pi</span><span class="p">)</span> <span class="o">/</span>
                                    <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="n">d</span><span class="p">)</span> <span class="o">/</span> <span class="n">mu</span><span class="p">))</span><span class="o">.</span><span class="n">limit_denominator</span><span class="p">()</span>
                <span class="n">m</span> <span class="o">=</span> <span class="n">fraction</span><span class="o">.</span><span class="n">denominator</span>
                <span class="n">n</span> <span class="o">=</span> <span class="n">fraction</span><span class="o">.</span><span class="n">numerator</span>

            <span class="c1"># construct the rotation matrix, check reference for details</span>
            <span class="n">r_list</span> <span class="o">=</span> <span class="p">[(</span><span class="n">mu</span> <span class="o">-</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span><span class="p">)</span> <span class="o">*</span> <span class="p">(</span><span class="n">u</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">-</span> <span class="n">v</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">-</span> <span class="n">w</span> <span class="o">**</span> <span class="mi">2</span><span class="p">)</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span>
                      <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="p">(</span><span class="n">v</span> <span class="o">-</span> <span class="n">w</span><span class="p">)</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span> <span class="o">-</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">v</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span>
                      <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">**</span> <span class="mi">2</span><span class="p">,</span>
                      <span class="mi">2</span> <span class="o">*</span> <span class="p">(</span><span class="n">mv</span> <span class="o">*</span> <span class="n">u</span> <span class="o">*</span> <span class="n">n</span> <span class="o">*</span> <span class="p">(</span><span class="n">w</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="n">u</span> <span class="o">*</span> <span class="n">n</span> <span class="o">-</span> <span class="n">m</span><span class="p">)</span> <span class="o">-</span> <span class="p">(</span><span class="n">mu</span> <span class="o">-</span> <span class="n">mv</span><span class="p">)</span> <span class="o">*</span>
                           <span class="n">m</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="p">(</span><span class="n">mu</span> <span class="o">-</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span><span class="p">)</span> <span class="o">*</span> <span class="n">u</span> <span class="o">*</span> <span class="n">v</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span><span class="p">),</span>
                      <span class="mi">2</span> <span class="o">*</span> <span class="p">(</span><span class="n">mv</span> <span class="o">*</span> <span class="n">u</span> <span class="o">*</span> <span class="n">n</span> <span class="o">*</span> <span class="p">(</span><span class="n">v</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="n">u</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="n">m</span><span class="p">)</span> <span class="o">+</span> <span class="p">(</span><span class="n">mu</span> <span class="o">-</span> <span class="n">mv</span><span class="p">)</span> <span class="o">*</span>
                           <span class="n">m</span> <span class="o">*</span> <span class="n">v</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="p">(</span><span class="n">mu</span> <span class="o">-</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span><span class="p">)</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">u</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span><span class="p">),</span>
                      <span class="mi">2</span> <span class="o">*</span> <span class="p">(</span><span class="n">mv</span> <span class="o">*</span> <span class="n">v</span> <span class="o">*</span> <span class="n">n</span> <span class="o">*</span> <span class="p">(</span><span class="n">w</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="n">v</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="n">m</span><span class="p">)</span> <span class="o">+</span> <span class="p">(</span><span class="n">mu</span> <span class="o">-</span> <span class="n">mv</span><span class="p">)</span> <span class="o">*</span>
                           <span class="n">m</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="p">(</span><span class="n">mu</span> <span class="o">-</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span><span class="p">)</span> <span class="o">*</span> <span class="n">u</span> <span class="o">*</span> <span class="n">v</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span><span class="p">),</span>
                      <span class="p">(</span><span class="n">mu</span> <span class="o">-</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span><span class="p">)</span> <span class="o">*</span> <span class="p">(</span><span class="n">v</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">-</span> <span class="n">w</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">-</span> <span class="n">u</span> <span class="o">**</span> <span class="mi">2</span><span class="p">)</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span>
                      <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="p">(</span><span class="n">w</span> <span class="o">-</span> <span class="n">u</span><span class="p">)</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span> <span class="o">-</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">u</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span>
                      <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">**</span> <span class="mi">2</span><span class="p">,</span>
                      <span class="mi">2</span> <span class="o">*</span> <span class="p">(</span><span class="n">mv</span> <span class="o">*</span> <span class="n">v</span> <span class="o">*</span> <span class="n">n</span> <span class="o">*</span> <span class="p">(</span><span class="n">v</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="n">u</span> <span class="o">*</span> <span class="n">n</span> <span class="o">-</span> <span class="n">m</span><span class="p">)</span> <span class="o">-</span> <span class="p">(</span><span class="n">mu</span> <span class="o">-</span> <span class="n">mv</span><span class="p">)</span> <span class="o">*</span>
                           <span class="n">m</span> <span class="o">*</span> <span class="n">u</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="p">(</span><span class="n">mu</span> <span class="o">-</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span><span class="p">)</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">v</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span><span class="p">),</span>
                      <span class="mi">2</span> <span class="o">*</span> <span class="p">(</span><span class="n">mv</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">n</span> <span class="o">*</span> <span class="p">(</span><span class="n">w</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="n">v</span> <span class="o">*</span> <span class="n">n</span> <span class="o">-</span> <span class="n">m</span><span class="p">)</span> <span class="o">-</span> <span class="p">(</span><span class="n">mu</span> <span class="o">-</span> <span class="n">mv</span><span class="p">)</span> <span class="o">*</span>
                           <span class="n">m</span> <span class="o">*</span> <span class="n">v</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="p">(</span><span class="n">mu</span> <span class="o">-</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span><span class="p">)</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">u</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span><span class="p">),</span>
                      <span class="mi">2</span> <span class="o">*</span> <span class="p">(</span><span class="n">mv</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">n</span> <span class="o">*</span> <span class="p">(</span><span class="n">w</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="n">u</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="n">m</span><span class="p">)</span> <span class="o">+</span> <span class="p">(</span><span class="n">mu</span> <span class="o">-</span> <span class="n">mv</span><span class="p">)</span> <span class="o">*</span>
                           <span class="n">m</span> <span class="o">*</span> <span class="n">u</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="p">(</span><span class="n">mu</span> <span class="o">-</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span><span class="p">)</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">v</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span><span class="p">),</span>
                      <span class="p">(</span><span class="n">mu</span> <span class="o">-</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span><span class="p">)</span> <span class="o">*</span> <span class="p">(</span><span class="n">w</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">-</span> <span class="n">u</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">-</span> <span class="n">v</span> <span class="o">**</span> <span class="mi">2</span><span class="p">)</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span>
                      <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="p">(</span><span class="n">u</span> <span class="o">-</span> <span class="n">v</span><span class="p">)</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span> <span class="o">-</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">u</span> <span class="o">*</span> <span class="n">v</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span>
                      <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">**</span> <span class="mi">2</span><span class="p">]</span>
            <span class="n">m</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span> <span class="o">*</span> <span class="n">m</span>
            <span class="n">r_list_inv</span> <span class="o">=</span> <span class="p">[(</span><span class="n">mu</span> <span class="o">-</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span><span class="p">)</span> <span class="o">*</span> <span class="p">(</span><span class="n">u</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">-</span> <span class="n">v</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">-</span> <span class="n">w</span> <span class="o">**</span> <span class="mi">2</span><span class="p">)</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span>
                          <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="p">(</span><span class="n">v</span> <span class="o">-</span> <span class="n">w</span><span class="p">)</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span> <span class="o">-</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">v</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span>
                          <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">**</span> <span class="mi">2</span><span class="p">,</span>
                          <span class="mi">2</span> <span class="o">*</span> <span class="p">(</span><span class="n">mv</span> <span class="o">*</span> <span class="n">u</span> <span class="o">*</span> <span class="n">n</span> <span class="o">*</span> <span class="p">(</span><span class="n">w</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="n">u</span> <span class="o">*</span> <span class="n">n</span> <span class="o">-</span> <span class="n">m</span><span class="p">)</span> <span class="o">-</span> <span class="p">(</span><span class="n">mu</span> <span class="o">-</span> <span class="n">mv</span><span class="p">)</span> <span class="o">*</span>
                               <span class="n">m</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="p">(</span><span class="n">mu</span> <span class="o">-</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span><span class="p">)</span> <span class="o">*</span> <span class="n">u</span> <span class="o">*</span> <span class="n">v</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span><span class="p">),</span>
                          <span class="mi">2</span> <span class="o">*</span> <span class="p">(</span><span class="n">mv</span> <span class="o">*</span> <span class="n">u</span> <span class="o">*</span> <span class="n">n</span> <span class="o">*</span> <span class="p">(</span><span class="n">v</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="n">u</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="n">m</span><span class="p">)</span> <span class="o">+</span> <span class="p">(</span><span class="n">mu</span> <span class="o">-</span> <span class="n">mv</span><span class="p">)</span> <span class="o">*</span>
                               <span class="n">m</span> <span class="o">*</span> <span class="n">v</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="p">(</span><span class="n">mu</span> <span class="o">-</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span><span class="p">)</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">u</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span><span class="p">),</span>
                          <span class="mi">2</span> <span class="o">*</span> <span class="p">(</span><span class="n">mv</span> <span class="o">*</span> <span class="n">v</span> <span class="o">*</span> <span class="n">n</span> <span class="o">*</span> <span class="p">(</span><span class="n">w</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="n">v</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="n">m</span><span class="p">)</span> <span class="o">+</span> <span class="p">(</span><span class="n">mu</span> <span class="o">-</span> <span class="n">mv</span><span class="p">)</span> <span class="o">*</span>
                               <span class="n">m</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="p">(</span><span class="n">mu</span> <span class="o">-</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span><span class="p">)</span> <span class="o">*</span> <span class="n">u</span> <span class="o">*</span> <span class="n">v</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span><span class="p">),</span>
                          <span class="p">(</span><span class="n">mu</span> <span class="o">-</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span><span class="p">)</span> <span class="o">*</span> <span class="p">(</span><span class="n">v</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">-</span> <span class="n">w</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">-</span> <span class="n">u</span> <span class="o">**</span> <span class="mi">2</span><span class="p">)</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span>
                          <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="p">(</span><span class="n">w</span> <span class="o">-</span> <span class="n">u</span><span class="p">)</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span> <span class="o">-</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">u</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span>
                          <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">**</span> <span class="mi">2</span><span class="p">,</span>
                          <span class="mi">2</span> <span class="o">*</span> <span class="p">(</span><span class="n">mv</span> <span class="o">*</span> <span class="n">v</span> <span class="o">*</span> <span class="n">n</span> <span class="o">*</span> <span class="p">(</span><span class="n">v</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="n">u</span> <span class="o">*</span> <span class="n">n</span> <span class="o">-</span> <span class="n">m</span><span class="p">)</span> <span class="o">-</span> <span class="p">(</span><span class="n">mu</span> <span class="o">-</span> <span class="n">mv</span><span class="p">)</span> <span class="o">*</span>
                               <span class="n">m</span> <span class="o">*</span> <span class="n">u</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="p">(</span><span class="n">mu</span> <span class="o">-</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span><span class="p">)</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">v</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span><span class="p">),</span>
                          <span class="mi">2</span> <span class="o">*</span> <span class="p">(</span><span class="n">mv</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">n</span> <span class="o">*</span> <span class="p">(</span><span class="n">w</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="n">v</span> <span class="o">*</span> <span class="n">n</span> <span class="o">-</span> <span class="n">m</span><span class="p">)</span> <span class="o">-</span> <span class="p">(</span><span class="n">mu</span> <span class="o">-</span> <span class="n">mv</span><span class="p">)</span> <span class="o">*</span>
                               <span class="n">m</span> <span class="o">*</span> <span class="n">v</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="p">(</span><span class="n">mu</span> <span class="o">-</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span><span class="p">)</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">u</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span><span class="p">),</span>
                          <span class="mi">2</span> <span class="o">*</span> <span class="p">(</span><span class="n">mv</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">n</span> <span class="o">*</span> <span class="p">(</span><span class="n">w</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="n">u</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="n">m</span><span class="p">)</span> <span class="o">+</span> <span class="p">(</span><span class="n">mu</span> <span class="o">-</span> <span class="n">mv</span><span class="p">)</span> <span class="o">*</span>
                               <span class="n">m</span> <span class="o">*</span> <span class="n">u</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="p">(</span><span class="n">mu</span> <span class="o">-</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span><span class="p">)</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">v</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span><span class="p">),</span>
                          <span class="p">(</span><span class="n">mu</span> <span class="o">-</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span><span class="p">)</span> <span class="o">*</span> <span class="p">(</span><span class="n">w</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">-</span> <span class="n">u</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">-</span> <span class="n">v</span> <span class="o">**</span> <span class="mi">2</span><span class="p">)</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span>
                          <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="p">(</span><span class="n">u</span> <span class="o">-</span> <span class="n">v</span><span class="p">)</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span> <span class="o">-</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">u</span> <span class="o">*</span> <span class="n">v</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span>
                          <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">**</span> <span class="mi">2</span><span class="p">]</span>
            <span class="n">m</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span> <span class="o">*</span> <span class="n">m</span>
            <span class="n">F</span> <span class="o">=</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span> <span class="n">d</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span>
            <span class="n">all_list</span> <span class="o">=</span> <span class="n">r_list_inv</span> <span class="o">+</span> <span class="n">r_list</span> <span class="o">+</span> <span class="p">[</span><span class="n">F</span><span class="p">]</span>
            <span class="n">com_fac</span> <span class="o">=</span> <span class="n">reduce</span><span class="p">(</span><span class="n">gcd</span><span class="p">,</span> <span class="n">all_list</span><span class="p">)</span>
            <span class="n">sigma</span> <span class="o">=</span> <span class="n">F</span> <span class="o">/</span> <span class="n">com_fac</span>
            <span class="n">r_matrix</span> <span class="o">=</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">r_list</span><span class="p">)</span> <span class="o">/</span> <span class="n">com_fac</span> <span class="o">/</span> <span class="n">sigma</span><span class="p">)</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">u</span><span class="p">,</span> <span class="n">v</span><span class="p">,</span> <span class="n">w</span> <span class="o">=</span> <span class="n">r_axis</span>
            <span class="k">if</span> <span class="n">lat_type</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="o">==</span> <span class="s1">&#39;c&#39;</span><span class="p">:</span>
                <span class="n">mu</span> <span class="o">=</span> <span class="mi">1</span>
                <span class="n">lam</span> <span class="o">=</span> <span class="mi">1</span>
                <span class="n">mv</span> <span class="o">=</span> <span class="mi">1</span>
            <span class="k">elif</span> <span class="n">lat_type</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="o">==</span> <span class="s1">&#39;t&#39;</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">ratio</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                    <span class="n">mu</span><span class="p">,</span> <span class="n">mv</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">]</span>
                    <span class="k">if</span> <span class="n">w</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">:</span>
                        <span class="k">if</span> <span class="n">u</span> <span class="o">!=</span> <span class="mi">0</span> <span class="ow">or</span> <span class="p">(</span><span class="n">v</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">):</span>
                            <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s1">&#39;For irrational c2/a2, CSL only exist for [0,0,1] &#39;</span>
                                               <span class="s1">&#39;or [u,v,0] and m = 0&#39;</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">mu</span><span class="p">,</span> <span class="n">mv</span> <span class="o">=</span> <span class="n">ratio</span>
                <span class="n">lam</span> <span class="o">=</span> <span class="n">mv</span>
            <span class="k">elif</span> <span class="n">lat_type</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="o">==</span> <span class="s1">&#39;o&#39;</span><span class="p">:</span>
                <span class="k">if</span> <span class="kc">None</span> <span class="ow">in</span> <span class="n">ratio</span><span class="p">:</span>
                    <span class="n">mu</span><span class="p">,</span> <span class="n">lam</span><span class="p">,</span> <span class="n">mv</span> <span class="o">=</span> <span class="n">ratio</span>
                    <span class="n">non_none</span> <span class="o">=</span> <span class="p">[</span><span class="n">i</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">ratio</span> <span class="k">if</span> <span class="n">i</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">]</span>
                    <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">non_none</span><span class="p">)</span> <span class="o">&lt;</span> <span class="mi">2</span><span class="p">:</span>
                        <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s1">&#39;No CSL exist for two irrational numbers&#39;</span><span class="p">)</span>
                    <span class="n">non1</span><span class="p">,</span> <span class="n">non2</span> <span class="o">=</span> <span class="n">non_none</span>
                    <span class="k">if</span> <span class="n">mu</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                        <span class="n">lam</span> <span class="o">=</span> <span class="n">non1</span>
                        <span class="n">mv</span> <span class="o">=</span> <span class="n">non2</span>
                        <span class="n">mu</span> <span class="o">=</span> <span class="mi">1</span>
                        <span class="k">if</span> <span class="n">w</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">:</span>
                            <span class="k">if</span> <span class="n">u</span> <span class="o">!=</span> <span class="mi">0</span> <span class="ow">or</span> <span class="p">(</span><span class="n">v</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">):</span>
                                <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s1">&#39;For irrational c2, CSL only exist for [0,0,1] &#39;</span>
                                                   <span class="s1">&#39;or [u,v,0] and m = 0&#39;</span><span class="p">)</span>
                    <span class="k">elif</span> <span class="n">lam</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                        <span class="n">mu</span> <span class="o">=</span> <span class="n">non1</span>
                        <span class="n">mv</span> <span class="o">=</span> <span class="n">non2</span>
                        <span class="n">lam</span> <span class="o">=</span> <span class="mi">1</span>
                        <span class="k">if</span> <span class="n">v</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">:</span>
                            <span class="k">if</span> <span class="n">u</span> <span class="o">!=</span> <span class="mi">0</span> <span class="ow">or</span> <span class="p">(</span><span class="n">w</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">):</span>
                                <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s1">&#39;For irrational b2, CSL only exist for [0,1,0] &#39;</span>
                                                   <span class="s1">&#39;or [u,0,w] and m = 0&#39;</span><span class="p">)</span>
                    <span class="k">elif</span> <span class="n">mv</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                        <span class="n">mu</span> <span class="o">=</span> <span class="n">non1</span>
                        <span class="n">lam</span> <span class="o">=</span> <span class="n">non2</span>
                        <span class="n">mv</span> <span class="o">=</span> <span class="mi">1</span>
                        <span class="k">if</span> <span class="n">u</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">:</span>
                            <span class="k">if</span> <span class="n">w</span> <span class="o">!=</span> <span class="mi">0</span> <span class="ow">or</span> <span class="p">(</span><span class="n">v</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">):</span>
                                <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s1">&#39;For irrational a2, CSL only exist for [1,0,0] &#39;</span>
                                                   <span class="s1">&#39;or [0,v,w] and m = 0&#39;</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">mu</span><span class="p">,</span> <span class="n">lam</span><span class="p">,</span> <span class="n">mv</span> <span class="o">=</span> <span class="n">ratio</span>
                    <span class="k">if</span> <span class="n">u</span> <span class="o">==</span> <span class="mi">0</span> <span class="ow">and</span> <span class="n">v</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                        <span class="n">mu</span> <span class="o">=</span> <span class="mi">1</span>
                    <span class="k">if</span> <span class="n">u</span> <span class="o">==</span> <span class="mi">0</span> <span class="ow">and</span> <span class="n">w</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                        <span class="n">lam</span> <span class="o">=</span> <span class="mi">1</span>
                    <span class="k">if</span> <span class="n">v</span> <span class="o">==</span> <span class="mi">0</span> <span class="ow">and</span> <span class="n">w</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                        <span class="n">mv</span> <span class="o">=</span> <span class="mi">1</span>

            <span class="c1"># make sure mu, lambda, mv are coprime integers.</span>
            <span class="k">if</span> <span class="n">reduce</span><span class="p">(</span><span class="n">gcd</span><span class="p">,</span> <span class="p">[</span><span class="n">mu</span><span class="p">,</span> <span class="n">lam</span><span class="p">,</span> <span class="n">mv</span><span class="p">])</span> <span class="o">!=</span> <span class="mi">1</span><span class="p">:</span>
                <span class="n">temp</span> <span class="o">=</span> <span class="n">reduce</span><span class="p">(</span><span class="n">gcd</span><span class="p">,</span> <span class="p">[</span><span class="n">mu</span><span class="p">,</span> <span class="n">lam</span><span class="p">,</span> <span class="n">mv</span><span class="p">])</span>
                <span class="n">mu</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">mu</span> <span class="o">/</span> <span class="n">temp</span><span class="p">))</span>
                <span class="n">mv</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">mv</span> <span class="o">/</span> <span class="n">temp</span><span class="p">))</span>
                <span class="n">lam</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">lam</span> <span class="o">/</span> <span class="n">temp</span><span class="p">))</span>
            <span class="n">d</span> <span class="o">=</span> <span class="p">(</span><span class="n">mv</span> <span class="o">*</span> <span class="n">u</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span> <span class="n">lam</span> <span class="o">*</span> <span class="n">v</span> <span class="o">**</span> <span class="mi">2</span><span class="p">)</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">+</span> <span class="n">w</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">mv</span>
            <span class="k">if</span> <span class="nb">abs</span><span class="p">(</span><span class="n">angle</span> <span class="o">-</span> <span class="mf">180.0</span><span class="p">)</span> <span class="o">&lt;</span> <span class="mf">1.e0</span><span class="p">:</span>
                <span class="n">m</span> <span class="o">=</span> <span class="mi">0</span>
                <span class="n">n</span> <span class="o">=</span> <span class="mi">1</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">fraction</span> <span class="o">=</span> <span class="n">Fraction</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">tan</span><span class="p">(</span><span class="n">angle</span> <span class="o">/</span> <span class="mi">2</span> <span class="o">/</span> <span class="mf">180.0</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">pi</span><span class="p">)</span> <span class="o">/</span>
                                    <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">d</span> <span class="o">/</span> <span class="n">mu</span> <span class="o">/</span> <span class="n">lam</span><span class="p">))</span><span class="o">.</span><span class="n">limit_denominator</span><span class="p">()</span>
                <span class="n">m</span> <span class="o">=</span> <span class="n">fraction</span><span class="o">.</span><span class="n">denominator</span>
                <span class="n">n</span> <span class="o">=</span> <span class="n">fraction</span><span class="o">.</span><span class="n">numerator</span>
            <span class="n">r_list</span> <span class="o">=</span> <span class="p">[(</span><span class="n">u</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">-</span> <span class="n">lam</span> <span class="o">*</span> <span class="n">v</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">-</span>
                       <span class="n">w</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">mv</span><span class="p">)</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span> <span class="n">lam</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">**</span> <span class="mi">2</span><span class="p">,</span>
                      <span class="mi">2</span> <span class="o">*</span> <span class="n">lam</span> <span class="o">*</span> <span class="p">(</span><span class="n">v</span> <span class="o">*</span> <span class="n">u</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">-</span> <span class="n">w</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span><span class="p">),</span>
                      <span class="mi">2</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="p">(</span><span class="n">u</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span> <span class="n">v</span> <span class="o">*</span> <span class="n">lam</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span><span class="p">),</span>
                      <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="p">(</span><span class="n">u</span> <span class="o">*</span> <span class="n">v</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span> <span class="n">w</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span><span class="p">),</span>
                      <span class="p">(</span><span class="n">v</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">lam</span> <span class="o">-</span> <span class="n">u</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">-</span>
                       <span class="n">w</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">mv</span><span class="p">)</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span> <span class="n">lam</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">**</span> <span class="mi">2</span><span class="p">,</span>
                      <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="p">(</span><span class="n">v</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">-</span> <span class="n">u</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span><span class="p">),</span>
                      <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="p">(</span><span class="n">u</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">-</span> <span class="n">v</span> <span class="o">*</span> <span class="n">lam</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span><span class="p">),</span>
                      <span class="mi">2</span> <span class="o">*</span> <span class="n">lam</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="p">(</span><span class="n">v</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span> <span class="n">u</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span><span class="p">),</span>
                      <span class="p">(</span><span class="n">w</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">-</span> <span class="n">u</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">-</span>
                       <span class="n">v</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">lam</span><span class="p">)</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span> <span class="n">lam</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">**</span> <span class="mi">2</span><span class="p">]</span>
            <span class="n">m</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span> <span class="o">*</span> <span class="n">m</span>
            <span class="n">r_list_inv</span> <span class="o">=</span> <span class="p">[(</span><span class="n">u</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">-</span> <span class="n">lam</span> <span class="o">*</span> <span class="n">v</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">-</span>
                           <span class="n">w</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">mv</span><span class="p">)</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span> <span class="n">lam</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">**</span> <span class="mi">2</span><span class="p">,</span>
                          <span class="mi">2</span> <span class="o">*</span> <span class="n">lam</span> <span class="o">*</span> <span class="p">(</span><span class="n">v</span> <span class="o">*</span> <span class="n">u</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">-</span> <span class="n">w</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span><span class="p">),</span>
                          <span class="mi">2</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="p">(</span><span class="n">u</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span> <span class="n">v</span> <span class="o">*</span> <span class="n">lam</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span><span class="p">),</span>
                          <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="p">(</span><span class="n">u</span> <span class="o">*</span> <span class="n">v</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span> <span class="n">w</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span><span class="p">),</span>
                          <span class="p">(</span><span class="n">v</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">lam</span> <span class="o">-</span> <span class="n">u</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">-</span>
                           <span class="n">w</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">mv</span><span class="p">)</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span> <span class="n">lam</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">**</span> <span class="mi">2</span><span class="p">,</span>
                          <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="p">(</span><span class="n">v</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">-</span> <span class="n">u</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span><span class="p">),</span>
                          <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="p">(</span><span class="n">u</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">-</span> <span class="n">v</span> <span class="o">*</span> <span class="n">lam</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span><span class="p">),</span>
                          <span class="mi">2</span> <span class="o">*</span> <span class="n">lam</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="p">(</span><span class="n">v</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span> <span class="n">u</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span><span class="p">),</span>
                          <span class="p">(</span><span class="n">w</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">-</span> <span class="n">u</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">-</span>
                           <span class="n">v</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">lam</span><span class="p">)</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span> <span class="n">lam</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">**</span> <span class="mi">2</span><span class="p">]</span>
            <span class="n">m</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span> <span class="o">*</span> <span class="n">m</span>
            <span class="n">F</span> <span class="o">=</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">lam</span> <span class="o">*</span> <span class="n">m</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span> <span class="n">d</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span>
            <span class="n">all_list</span> <span class="o">=</span> <span class="n">r_list</span> <span class="o">+</span> <span class="n">r_list_inv</span> <span class="o">+</span> <span class="p">[</span><span class="n">F</span><span class="p">]</span>
            <span class="n">com_fac</span> <span class="o">=</span> <span class="n">reduce</span><span class="p">(</span><span class="n">gcd</span><span class="p">,</span> <span class="n">all_list</span><span class="p">)</span>
            <span class="n">sigma</span> <span class="o">=</span> <span class="n">F</span> <span class="o">/</span> <span class="n">com_fac</span>
            <span class="n">r_matrix</span> <span class="o">=</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">r_list</span><span class="p">)</span> <span class="o">/</span> <span class="n">com_fac</span> <span class="o">/</span> <span class="n">sigma</span><span class="p">)</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>

        <span class="k">if</span> <span class="p">(</span><span class="n">sigma</span> <span class="o">&gt;</span> <span class="mi">1000</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s1">&#39;Sigma &gt;1000 too large. Are you sure what you are doing, &#39;</span>
                               <span class="s1">&#39;Please check the GB if exist&#39;</span><span class="p">)</span>
        <span class="c1"># transform surface, r_axis, r_matrix in terms of primitive lattice</span>
        <span class="n">surface</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">matmul</span><span class="p">(</span><span class="n">surface</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">transpose</span><span class="p">(</span><span class="n">trans_cry</span><span class="p">))</span>
        <span class="n">fractions</span> <span class="o">=</span> <span class="p">[</span><span class="n">Fraction</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">.</span><span class="n">limit_denominator</span><span class="p">()</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">surface</span><span class="p">]</span>
        <span class="n">least_mul</span> <span class="o">=</span> <span class="n">reduce</span><span class="p">(</span><span class="n">lcm</span><span class="p">,</span> <span class="p">[</span><span class="n">f</span><span class="o">.</span><span class="n">denominator</span> <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">fractions</span><span class="p">])</span>
        <span class="n">surface</span> <span class="o">=</span> <span class="p">[</span><span class="nb">int</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">x</span> <span class="o">*</span> <span class="n">least_mul</span><span class="p">))</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">surface</span><span class="p">]</span>
        <span class="k">if</span> <span class="n">reduce</span><span class="p">(</span><span class="n">gcd</span><span class="p">,</span> <span class="n">surface</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">1</span><span class="p">:</span>
            <span class="n">index</span> <span class="o">=</span> <span class="n">reduce</span><span class="p">(</span><span class="n">gcd</span><span class="p">,</span> <span class="n">surface</span><span class="p">)</span>
            <span class="n">surface</span> <span class="o">=</span> <span class="p">[</span><span class="nb">int</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">x</span> <span class="o">/</span> <span class="n">index</span><span class="p">))</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">surface</span><span class="p">]</span>
        <span class="n">r_axis</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">rint</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">matmul</span><span class="p">(</span><span class="n">r_axis</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">inv</span><span class="p">(</span><span class="n">trans_cry</span><span class="p">)))</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="nb">int</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">reduce</span><span class="p">(</span><span class="n">gcd</span><span class="p">,</span> <span class="n">r_axis</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">1</span><span class="p">:</span>
            <span class="n">r_axis</span> <span class="o">=</span> <span class="p">[</span><span class="nb">int</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">x</span> <span class="o">/</span> <span class="n">reduce</span><span class="p">(</span><span class="n">gcd</span><span class="p">,</span> <span class="n">r_axis</span><span class="p">)))</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">r_axis</span><span class="p">]</span>
        <span class="n">r_matrix</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">inv</span><span class="p">(</span><span class="n">trans_cry</span><span class="o">.</span><span class="n">T</span><span class="p">),</span> <span class="n">r_matrix</span><span class="p">),</span> <span class="n">trans_cry</span><span class="o">.</span><span class="n">T</span><span class="p">)</span>
        <span class="c1"># set one vector of the basis to the rotation axis direction, and</span>
        <span class="c1"># obtain the corresponding transform matrix</span>
        <span class="n">eye</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">eye</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">int</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">h</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">3</span><span class="p">):</span>
            <span class="k">if</span> <span class="nb">abs</span><span class="p">(</span><span class="n">r_axis</span><span class="p">[</span><span class="n">h</span><span class="p">])</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">eye</span><span class="p">[</span><span class="n">h</span><span class="p">]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">r_axis</span><span class="p">)</span>
                <span class="n">k</span> <span class="o">=</span> <span class="n">h</span> <span class="o">+</span> <span class="mi">1</span> <span class="k">if</span> <span class="n">h</span> <span class="o">+</span> <span class="mi">1</span> <span class="o">&lt;</span> <span class="mi">3</span> <span class="k">else</span> <span class="nb">abs</span><span class="p">(</span><span class="mi">2</span> <span class="o">-</span> <span class="n">h</span><span class="p">)</span>
                <span class="n">l</span> <span class="o">=</span> <span class="n">h</span> <span class="o">+</span> <span class="mi">2</span> <span class="k">if</span> <span class="n">h</span> <span class="o">+</span> <span class="mi">2</span> <span class="o">&lt;</span> <span class="mi">3</span> <span class="k">else</span> <span class="nb">abs</span><span class="p">(</span><span class="mi">1</span> <span class="o">-</span> <span class="n">h</span><span class="p">)</span>
                <span class="k">break</span>
        <span class="n">trans</span> <span class="o">=</span> <span class="n">eye</span><span class="o">.</span><span class="n">T</span>
        <span class="n">new_rot</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">r_matrix</span><span class="p">)</span>

        <span class="c1"># with the rotation matrix to construct the CSL lattice, check reference for details</span>
        <span class="n">fractions</span> <span class="o">=</span> <span class="p">[</span><span class="n">Fraction</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">.</span><span class="n">limit_denominator</span><span class="p">()</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">new_rot</span><span class="p">[:,</span> <span class="n">k</span><span class="p">]]</span>
        <span class="n">least_mul</span> <span class="o">=</span> <span class="n">reduce</span><span class="p">(</span><span class="n">lcm</span><span class="p">,</span> <span class="p">[</span><span class="n">f</span><span class="o">.</span><span class="n">denominator</span> <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">fractions</span><span class="p">])</span>
        <span class="n">scale</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">))</span>
        <span class="n">scale</span><span class="p">[</span><span class="n">h</span><span class="p">,</span> <span class="n">h</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span>
        <span class="n">scale</span><span class="p">[</span><span class="n">k</span><span class="p">,</span> <span class="n">k</span><span class="p">]</span> <span class="o">=</span> <span class="n">least_mul</span>
        <span class="n">scale</span><span class="p">[</span><span class="n">l</span><span class="p">,</span> <span class="n">l</span><span class="p">]</span> <span class="o">=</span> <span class="n">sigma</span> <span class="o">/</span> <span class="n">least_mul</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">least_mul</span><span class="p">):</span>
            <span class="n">check_int</span> <span class="o">=</span> <span class="n">i</span> <span class="o">*</span> <span class="n">new_rot</span><span class="p">[:,</span> <span class="n">k</span><span class="p">]</span> <span class="o">+</span> <span class="p">(</span><span class="n">sigma</span> <span class="o">/</span> <span class="n">least_mul</span><span class="p">)</span> <span class="o">*</span> <span class="n">new_rot</span><span class="p">[:,</span> <span class="n">l</span><span class="p">]</span>
            <span class="k">if</span> <span class="nb">all</span><span class="p">([</span><span class="n">np</span><span class="o">.</span><span class="n">round</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="mi">5</span><span class="p">)</span><span class="o">.</span><span class="n">is_integer</span><span class="p">()</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="nb">list</span><span class="p">(</span><span class="n">check_int</span><span class="p">)]):</span>
                <span class="n">n_final</span> <span class="o">=</span> <span class="n">i</span>
                <span class="k">break</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">n_final</span>
        <span class="k">except</span> <span class="ne">NameError</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s1">&#39;Something is wrong. Check if this GB exists or not&#39;</span><span class="p">)</span>
        <span class="n">scale</span><span class="p">[</span><span class="n">k</span><span class="p">,</span> <span class="n">l</span><span class="p">]</span> <span class="o">=</span> <span class="n">n_final</span>
        <span class="c1"># each row of mat_csl is the CSL lattice vector</span>
        <span class="n">csl_init</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">rint</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">r_matrix</span><span class="p">,</span> <span class="n">trans</span><span class="p">),</span> <span class="n">scale</span><span class="p">))</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="nb">int</span><span class="p">)</span><span class="o">.</span><span class="n">T</span>
        <span class="k">if</span> <span class="nb">abs</span><span class="p">(</span><span class="n">r_axis</span><span class="p">[</span><span class="n">h</span><span class="p">])</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
            <span class="n">csl_init</span> <span class="o">=</span> <span class="n">GrainBoundaryGenerator</span><span class="o">.</span><span class="n">reduce_mat</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">csl_init</span><span class="p">),</span> <span class="n">r_axis</span><span class="p">[</span><span class="n">h</span><span class="p">],</span> <span class="n">r_matrix</span><span class="p">)</span>
        <span class="n">csl</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">rint</span><span class="p">(</span><span class="n">Lattice</span><span class="p">(</span><span class="n">csl_init</span><span class="p">)</span><span class="o">.</span><span class="n">get_niggli_reduced_lattice</span><span class="p">()</span><span class="o">.</span><span class="n">matrix</span><span class="p">)</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="nb">int</span><span class="p">)</span>

        <span class="c1"># find the best slab supercell in terms of the conventional cell from the csl lattice,</span>
        <span class="c1"># which is the transformation matrix</span>

        <span class="c1"># now trans_cry is the transformation matrix from crystal to cartesian coordinates.</span>
        <span class="c1"># for cubic, do not need to change.</span>
        <span class="k">if</span> <span class="n">lat_type</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="o">!=</span> <span class="s1">&#39;c&#39;</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">lat_type</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="o">==</span> <span class="s1">&#39;h&#39;</span><span class="p">:</span>
                <span class="n">trans_cry</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="o">-</span><span class="mf">0.5</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="mf">3.0</span><span class="p">)</span> <span class="o">/</span> <span class="mf">2.0</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span>
                                      <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">mu</span> <span class="o">/</span> <span class="n">mv</span><span class="p">)]])</span>
            <span class="k">elif</span> <span class="n">lat_type</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="o">==</span> <span class="s1">&#39;r&#39;</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">ratio</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                    <span class="n">c2_a2_ratio</span> <span class="o">=</span> <span class="mi">1</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">c2_a2_ratio</span> <span class="o">=</span> <span class="mf">3.0</span> <span class="o">/</span> <span class="p">(</span><span class="mi">2</span> <span class="o">-</span> <span class="mi">6</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">/</span> <span class="n">mu</span><span class="p">)</span>
                <span class="n">trans_cry</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([[</span><span class="mf">0.5</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="mf">3.0</span><span class="p">)</span> <span class="o">/</span> <span class="mf">6.0</span><span class="p">,</span> <span class="mf">1.0</span> <span class="o">/</span> <span class="mi">3</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">c2_a2_ratio</span><span class="p">)],</span>
                                      <span class="p">[</span><span class="o">-</span><span class="mf">0.5</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="mf">3.0</span><span class="p">)</span> <span class="o">/</span> <span class="mf">6.0</span><span class="p">,</span> <span class="mf">1.0</span> <span class="o">/</span> <span class="mi">3</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">c2_a2_ratio</span><span class="p">)],</span>
                                      <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="mf">3.0</span><span class="p">)</span> <span class="o">/</span> <span class="mf">3.0</span><span class="p">,</span> <span class="mf">1.0</span> <span class="o">/</span> <span class="mi">3</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">c2_a2_ratio</span><span class="p">)]])</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">trans_cry</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">lam</span> <span class="o">/</span> <span class="n">mv</span><span class="p">),</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">mu</span> <span class="o">/</span> <span class="n">mv</span><span class="p">)]])</span>
        <span class="n">t1_final</span> <span class="o">=</span> <span class="n">GrainBoundaryGenerator</span><span class="o">.</span><span class="n">slab_from_csl</span><span class="p">(</span><span class="n">csl</span><span class="p">,</span> <span class="n">surface</span><span class="p">,</span> <span class="n">normal</span><span class="p">,</span> <span class="n">trans_cry</span><span class="p">,</span> <span class="n">max_search</span><span class="o">=</span><span class="n">max_search</span><span class="p">,</span>
                                                        <span class="n">quick_gen</span><span class="o">=</span><span class="n">quick_gen</span><span class="p">)</span>
        <span class="n">t2_final</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">rint</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">t1_final</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">inv</span><span class="p">(</span><span class="n">r_matrix</span><span class="o">.</span><span class="n">T</span><span class="p">))))</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="nb">int</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">t1_final</span><span class="p">,</span> <span class="n">t2_final</span></div>

<div class="viewcode-block" id="GrainBoundaryGenerator.enum_sigma_cubic"><a class="viewcode-back" href="../../../../pymatgen.analysis.gb.grain.html#pymatgen.analysis.gb.grain.GrainBoundaryGenerator.enum_sigma_cubic">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">enum_sigma_cubic</span><span class="p">(</span><span class="n">cutoff</span><span class="p">,</span> <span class="n">r_axis</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Find all possible sigma values and corresponding rotation angles</span>
<span class="sd">        within a sigma value cutoff with known rotation axis in cubic system.</span>
<span class="sd">        The algorithm for this code is from reference, Acta Cryst, A40,108(1984)</span>
<span class="sd">        Args:</span>
<span class="sd">            cutoff (integer): the cutoff of sigma values.</span>
<span class="sd">            r_axis (list of three integers, e.g. u, v, w):</span>
<span class="sd">                    the rotation axis of the grain boundary, with the format of [u,v,w].</span>
<span class="sd">        Returns:</span>
<span class="sd">            sigmas (dict):</span>
<span class="sd">                    dictionary with keys as the possible integer sigma values</span>
<span class="sd">                    and values as list of the possible rotation angles to the</span>
<span class="sd">                    corresponding sigma values.</span>
<span class="sd">                    e.g. the format as</span>
<span class="sd">                    {sigma1: [angle11,angle12,...], sigma2: [angle21, angle22,...],...}</span>
<span class="sd">                    Note: the angles are the rotation angles of one grain respect to</span>
<span class="sd">                    the other grain.</span>
<span class="sd">                    When generate the microstructures of the grain boundary using these angles,</span>
<span class="sd">                    you need to analyze the symmetry of the structure. Different angles may</span>
<span class="sd">                    result in equivalent microstructures.</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">sigmas</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="c1"># make sure gcd(r_axis)==1</span>
        <span class="k">if</span> <span class="n">reduce</span><span class="p">(</span><span class="n">gcd</span><span class="p">,</span> <span class="n">r_axis</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">1</span><span class="p">:</span>
            <span class="n">r_axis</span> <span class="o">=</span> <span class="p">[</span><span class="nb">int</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">x</span> <span class="o">/</span> <span class="n">reduce</span><span class="p">(</span><span class="n">gcd</span><span class="p">,</span> <span class="n">r_axis</span><span class="p">)))</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">r_axis</span><span class="p">]</span>

        <span class="c1"># count the number of odds in r_axis</span>
        <span class="n">odd_r</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="nb">filter</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span> <span class="o">%</span> <span class="mi">2</span> <span class="o">==</span> <span class="mi">1</span><span class="p">,</span> <span class="n">r_axis</span><span class="p">)))</span>
        <span class="c1"># Compute the max n we need to enumerate.</span>
        <span class="k">if</span> <span class="n">odd_r</span> <span class="o">==</span> <span class="mi">3</span><span class="p">:</span>
            <span class="n">a_max</span> <span class="o">=</span> <span class="mi">4</span>
        <span class="k">elif</span> <span class="n">odd_r</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="n">a_max</span> <span class="o">=</span> <span class="mi">1</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">a_max</span> <span class="o">=</span> <span class="mi">2</span>
        <span class="n">n_max</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">cutoff</span> <span class="o">*</span> <span class="n">a_max</span> <span class="o">/</span> <span class="nb">sum</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">r_axis</span><span class="p">)</span> <span class="o">**</span> <span class="mi">2</span><span class="p">)))</span>
        <span class="c1"># enumerate all possible n, m to give possible sigmas within the cutoff.</span>
        <span class="k">for</span> <span class="n">n_loop</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">n_max</span> <span class="o">+</span> <span class="mi">1</span><span class="p">):</span>
            <span class="n">n</span> <span class="o">=</span> <span class="n">n_loop</span>
            <span class="n">m_max</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">cutoff</span> <span class="o">*</span> <span class="n">a_max</span> <span class="o">-</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="nb">sum</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">r_axis</span><span class="p">)</span> <span class="o">**</span> <span class="mi">2</span><span class="p">)))</span>
            <span class="k">for</span> <span class="n">m</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">m_max</span> <span class="o">+</span> <span class="mi">1</span><span class="p">):</span>
                <span class="k">if</span> <span class="n">gcd</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span> <span class="ow">or</span> <span class="n">m</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                    <span class="k">if</span> <span class="n">m</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                        <span class="n">n</span> <span class="o">=</span> <span class="mi">1</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="n">n</span> <span class="o">=</span> <span class="n">n_loop</span>
                    <span class="c1"># construct the quadruple [m, U,V,W], count the number of odds in</span>
                    <span class="c1"># quadruple to determine the parameter a, refer to the reference</span>
                    <span class="n">quadruple</span> <span class="o">=</span> <span class="p">[</span><span class="n">m</span><span class="p">]</span> <span class="o">+</span> <span class="p">[</span><span class="n">x</span> <span class="o">*</span> <span class="n">n</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">r_axis</span><span class="p">]</span>
                    <span class="n">odd_qua</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="nb">filter</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span> <span class="o">%</span> <span class="mi">2</span> <span class="o">==</span> <span class="mi">1</span><span class="p">,</span> <span class="n">quadruple</span><span class="p">)))</span>
                    <span class="k">if</span> <span class="n">odd_qua</span> <span class="o">==</span> <span class="mi">4</span><span class="p">:</span>
                        <span class="n">a</span> <span class="o">=</span> <span class="mi">4</span>
                    <span class="k">elif</span> <span class="n">odd_qua</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
                        <span class="n">a</span> <span class="o">=</span> <span class="mi">2</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="n">a</span> <span class="o">=</span> <span class="mi">1</span>
                    <span class="n">sigma</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="nb">round</span><span class="p">((</span><span class="n">m</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="nb">sum</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">r_axis</span><span class="p">)</span> <span class="o">**</span> <span class="mi">2</span><span class="p">))</span> <span class="o">/</span> <span class="n">a</span><span class="p">))</span>
                    <span class="k">if</span> <span class="p">(</span><span class="n">sigma</span> <span class="o">&lt;=</span> <span class="n">cutoff</span><span class="p">)</span> <span class="ow">and</span> <span class="p">(</span><span class="n">sigma</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">):</span>
                        <span class="k">if</span> <span class="n">sigma</span> <span class="ow">not</span> <span class="ow">in</span> <span class="nb">list</span><span class="p">(</span><span class="n">sigmas</span><span class="o">.</span><span class="n">keys</span><span class="p">()):</span>
                            <span class="k">if</span> <span class="n">m</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                                <span class="n">angle</span> <span class="o">=</span> <span class="mf">180.0</span>
                            <span class="k">else</span><span class="p">:</span>
                                <span class="n">angle</span> <span class="o">=</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">arctan</span><span class="p">(</span><span class="n">n</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="nb">sum</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">r_axis</span><span class="p">)</span> <span class="o">**</span> <span class="mi">2</span><span class="p">))</span> <span class="o">/</span> <span class="n">m</span><span class="p">)</span> \
                                        <span class="o">/</span> <span class="n">np</span><span class="o">.</span><span class="n">pi</span> <span class="o">*</span> <span class="mi">180</span>
                            <span class="n">sigmas</span><span class="p">[</span><span class="n">sigma</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="n">angle</span><span class="p">]</span>
                        <span class="k">else</span><span class="p">:</span>
                            <span class="k">if</span> <span class="n">m</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                                <span class="n">angle</span> <span class="o">=</span> <span class="mf">180.0</span>
                            <span class="k">else</span><span class="p">:</span>
                                <span class="n">angle</span> <span class="o">=</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">arctan</span><span class="p">(</span><span class="n">n</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="nb">sum</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">r_axis</span><span class="p">)</span> <span class="o">**</span> <span class="mi">2</span><span class="p">))</span> <span class="o">/</span> <span class="n">m</span><span class="p">)</span> \
                                        <span class="o">/</span> <span class="n">np</span><span class="o">.</span><span class="n">pi</span> <span class="o">*</span> <span class="mi">180</span>
                            <span class="k">if</span> <span class="n">angle</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">sigmas</span><span class="p">[</span><span class="n">sigma</span><span class="p">]:</span>
                                <span class="n">sigmas</span><span class="p">[</span><span class="n">sigma</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">angle</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">sigmas</span></div>

<div class="viewcode-block" id="GrainBoundaryGenerator.enum_sigma_hex"><a class="viewcode-back" href="../../../../pymatgen.analysis.gb.grain.html#pymatgen.analysis.gb.grain.GrainBoundaryGenerator.enum_sigma_hex">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">enum_sigma_hex</span><span class="p">(</span><span class="n">cutoff</span><span class="p">,</span> <span class="n">r_axis</span><span class="p">,</span> <span class="n">c2_a2_ratio</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Find all possible sigma values and corresponding rotation angles</span>
<span class="sd">        within a sigma value cutoff with known rotation axis in hexagonal system.</span>
<span class="sd">        The algorithm for this code is from reference, Acta Cryst, A38,550(1982)</span>

<span class="sd">        Args:</span>
<span class="sd">            cutoff (integer): the cutoff of sigma values.</span>
<span class="sd">            r_axis (list of three integers, e.g. u, v, w</span>
<span class="sd">                    or four integers, e.g. u, v, t, w):</span>
<span class="sd">                    the rotation axis of the grain boundary.</span>
<span class="sd">            c2_a2_ratio (list of two integers, e.g. mu, mv):</span>
<span class="sd">                    mu/mv is the square of the hexagonal axial ratio, which is rational</span>
<span class="sd">                    number. If irrational, set c2_a2_ratio = None</span>
<span class="sd">        Returns:</span>
<span class="sd">            sigmas (dict):</span>
<span class="sd">                    dictionary with keys as the possible integer sigma values</span>
<span class="sd">                    and values as list of the possible rotation angles to the</span>
<span class="sd">                    corresponding sigma values.</span>
<span class="sd">                    e.g. the format as</span>
<span class="sd">                    {sigma1: [angle11,angle12,...], sigma2: [angle21, angle22,...],...}</span>
<span class="sd">                    Note: the angles are the rotation angle of one grain respect to the</span>
<span class="sd">                    other grain.</span>
<span class="sd">                    When generate the microstructure of the grain boundary using these</span>
<span class="sd">                    angles, you need to analyze the symmetry of the structure. Different</span>
<span class="sd">                    angles may result in equivalent microstructures.</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">sigmas</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="c1"># make sure gcd(r_axis)==1</span>
        <span class="k">if</span> <span class="n">reduce</span><span class="p">(</span><span class="n">gcd</span><span class="p">,</span> <span class="n">r_axis</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">1</span><span class="p">:</span>
            <span class="n">r_axis</span> <span class="o">=</span> <span class="p">[</span><span class="nb">int</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">x</span> <span class="o">/</span> <span class="n">reduce</span><span class="p">(</span><span class="n">gcd</span><span class="p">,</span> <span class="n">r_axis</span><span class="p">)))</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">r_axis</span><span class="p">]</span>
        <span class="c1"># transform four index notation to three index notation</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">r_axis</span><span class="p">)</span> <span class="o">==</span> <span class="mi">4</span><span class="p">:</span>
            <span class="n">u1</span> <span class="o">=</span> <span class="n">r_axis</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
            <span class="n">v1</span> <span class="o">=</span> <span class="n">r_axis</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
            <span class="n">w1</span> <span class="o">=</span> <span class="n">r_axis</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span>
            <span class="n">u</span> <span class="o">=</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">u1</span> <span class="o">+</span> <span class="n">v1</span>
            <span class="n">v</span> <span class="o">=</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">v1</span> <span class="o">+</span> <span class="n">u1</span>
            <span class="n">w</span> <span class="o">=</span> <span class="n">w1</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">u</span><span class="p">,</span> <span class="n">v</span><span class="p">,</span> <span class="n">w</span> <span class="o">=</span> <span class="n">r_axis</span>

        <span class="c1"># make sure mu, mv are coprime integers.</span>
        <span class="k">if</span> <span class="n">c2_a2_ratio</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">mu</span><span class="p">,</span> <span class="n">mv</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">]</span>
            <span class="k">if</span> <span class="n">w</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">u</span> <span class="o">!=</span> <span class="mi">0</span> <span class="ow">or</span> <span class="p">(</span><span class="n">v</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">):</span>
                    <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s1">&#39;For irrational c2/a2, CSL only exist for [0,0,1] &#39;</span>
                                       <span class="s1">&#39;or [u,v,0] and m = 0&#39;</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">mu</span><span class="p">,</span> <span class="n">mv</span> <span class="o">=</span> <span class="n">c2_a2_ratio</span>
            <span class="k">if</span> <span class="n">gcd</span><span class="p">(</span><span class="n">mu</span><span class="p">,</span> <span class="n">mv</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">1</span><span class="p">:</span>
                <span class="n">temp</span> <span class="o">=</span> <span class="n">gcd</span><span class="p">(</span><span class="n">mu</span><span class="p">,</span> <span class="n">mv</span><span class="p">)</span>
                <span class="n">mu</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">mu</span> <span class="o">/</span> <span class="n">temp</span><span class="p">))</span>
                <span class="n">mv</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">mv</span> <span class="o">/</span> <span class="n">temp</span><span class="p">))</span>

        <span class="c1"># refer to the meaning of d in reference</span>
        <span class="n">d</span> <span class="o">=</span> <span class="p">(</span><span class="n">u</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span> <span class="n">v</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">-</span> <span class="n">u</span> <span class="o">*</span> <span class="n">v</span><span class="p">)</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">+</span> <span class="n">w</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mu</span>

        <span class="c1"># Compute the max n we need to enumerate.</span>
        <span class="n">n_max</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">((</span><span class="n">cutoff</span> <span class="o">*</span> <span class="mi">12</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">mv</span><span class="p">)</span> <span class="o">/</span> <span class="nb">abs</span><span class="p">(</span><span class="n">d</span><span class="p">)))</span>

        <span class="c1"># Enumerate all possible n, m to give possible sigmas within the cutoff.</span>
        <span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">n_max</span> <span class="o">+</span> <span class="mi">1</span><span class="p">):</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">c2_a2_ratio</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">)</span> <span class="ow">and</span> <span class="n">w</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">m_max</span> <span class="o">=</span> <span class="mi">0</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">m_max</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">((</span><span class="n">cutoff</span> <span class="o">*</span> <span class="mi">12</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">-</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">d</span><span class="p">)</span> <span class="o">/</span> <span class="p">(</span><span class="mi">3</span> <span class="o">*</span> <span class="n">mu</span><span class="p">)))</span>
            <span class="k">for</span> <span class="n">m</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">m_max</span> <span class="o">+</span> <span class="mi">1</span><span class="p">):</span>
                <span class="k">if</span> <span class="n">gcd</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span> <span class="ow">or</span> <span class="n">m</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                    <span class="c1"># construct the rotation matrix, refer to the reference</span>
                    <span class="n">R_list</span> <span class="o">=</span> <span class="p">[(</span><span class="n">u</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">-</span> <span class="n">v</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">-</span> <span class="n">w</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mu</span><span class="p">)</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span>
                              <span class="mi">2</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="mi">3</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">**</span> <span class="mi">2</span><span class="p">,</span>
                              <span class="p">(</span><span class="mi">2</span> <span class="o">*</span> <span class="n">v</span> <span class="o">-</span> <span class="n">u</span><span class="p">)</span> <span class="o">*</span> <span class="n">u</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">-</span> <span class="mi">4</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span><span class="p">,</span>
                              <span class="mi">2</span> <span class="o">*</span> <span class="n">u</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span> <span class="mi">2</span> <span class="o">*</span> <span class="p">(</span><span class="mi">2</span> <span class="o">*</span> <span class="n">v</span> <span class="o">-</span> <span class="n">u</span><span class="p">)</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span><span class="p">,</span>
                              <span class="p">(</span><span class="mi">2</span> <span class="o">*</span> <span class="n">u</span> <span class="o">-</span> <span class="n">v</span><span class="p">)</span> <span class="o">*</span> <span class="n">v</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span> <span class="mi">4</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span><span class="p">,</span>
                              <span class="p">(</span><span class="n">v</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">-</span> <span class="n">u</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">-</span> <span class="n">w</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mu</span><span class="p">)</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">-</span>
                              <span class="mi">2</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="mi">3</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">**</span> <span class="mi">2</span><span class="p">,</span>
                              <span class="mi">2</span> <span class="o">*</span> <span class="n">v</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">-</span> <span class="mi">2</span> <span class="o">*</span> <span class="p">(</span><span class="mi">2</span> <span class="o">*</span> <span class="n">u</span> <span class="o">-</span> <span class="n">v</span><span class="p">)</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span><span class="p">,</span>
                              <span class="p">(</span><span class="mi">2</span> <span class="o">*</span> <span class="n">u</span> <span class="o">-</span> <span class="n">v</span><span class="p">)</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">-</span> <span class="mi">3</span> <span class="o">*</span> <span class="n">v</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span><span class="p">,</span>
                              <span class="p">(</span><span class="mi">2</span> <span class="o">*</span> <span class="n">v</span> <span class="o">-</span> <span class="n">u</span><span class="p">)</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span> <span class="mi">3</span> <span class="o">*</span> <span class="n">u</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span><span class="p">,</span>
                              <span class="p">(</span><span class="n">w</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">-</span> <span class="n">u</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">-</span> <span class="n">v</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">+</span> <span class="n">u</span> <span class="o">*</span> <span class="n">v</span> <span class="o">*</span> <span class="n">mv</span><span class="p">)</span> <span class="o">*</span>
                              <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span> <span class="mi">3</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">**</span> <span class="mi">2</span><span class="p">]</span>
                    <span class="n">m</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span> <span class="o">*</span> <span class="n">m</span>
                    <span class="c1"># inverse of the rotation matrix</span>
                    <span class="n">R_list_inv</span> <span class="o">=</span> <span class="p">[(</span><span class="n">u</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">-</span> <span class="n">v</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">-</span> <span class="n">w</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mu</span><span class="p">)</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span>
                                  <span class="mi">2</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="mi">3</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">**</span> <span class="mi">2</span><span class="p">,</span>
                                  <span class="p">(</span><span class="mi">2</span> <span class="o">*</span> <span class="n">v</span> <span class="o">-</span> <span class="n">u</span><span class="p">)</span> <span class="o">*</span> <span class="n">u</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">-</span> <span class="mi">4</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span><span class="p">,</span>
                                  <span class="mi">2</span> <span class="o">*</span> <span class="n">u</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span> <span class="mi">2</span> <span class="o">*</span> <span class="p">(</span><span class="mi">2</span> <span class="o">*</span> <span class="n">v</span> <span class="o">-</span> <span class="n">u</span><span class="p">)</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span><span class="p">,</span>
                                  <span class="p">(</span><span class="mi">2</span> <span class="o">*</span> <span class="n">u</span> <span class="o">-</span> <span class="n">v</span><span class="p">)</span> <span class="o">*</span> <span class="n">v</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span> <span class="mi">4</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span><span class="p">,</span>
                                  <span class="p">(</span><span class="n">v</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">-</span> <span class="n">u</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">-</span> <span class="n">w</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mu</span><span class="p">)</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">-</span>
                                  <span class="mi">2</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="mi">3</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">**</span> <span class="mi">2</span><span class="p">,</span>
                                  <span class="mi">2</span> <span class="o">*</span> <span class="n">v</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">-</span> <span class="mi">2</span> <span class="o">*</span> <span class="p">(</span><span class="mi">2</span> <span class="o">*</span> <span class="n">u</span> <span class="o">-</span> <span class="n">v</span><span class="p">)</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span><span class="p">,</span>
                                  <span class="p">(</span><span class="mi">2</span> <span class="o">*</span> <span class="n">u</span> <span class="o">-</span> <span class="n">v</span><span class="p">)</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">-</span> <span class="mi">3</span> <span class="o">*</span> <span class="n">v</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span><span class="p">,</span>
                                  <span class="p">(</span><span class="mi">2</span> <span class="o">*</span> <span class="n">v</span> <span class="o">-</span> <span class="n">u</span><span class="p">)</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span> <span class="mi">3</span> <span class="o">*</span> <span class="n">u</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span><span class="p">,</span>
                                  <span class="p">(</span><span class="n">w</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">-</span> <span class="n">u</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">-</span> <span class="n">v</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">+</span> <span class="n">u</span> <span class="o">*</span> <span class="n">v</span> <span class="o">*</span> <span class="n">mv</span><span class="p">)</span> <span class="o">*</span>
                                  <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span> <span class="mi">3</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">**</span> <span class="mi">2</span><span class="p">]</span>
                    <span class="n">m</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span> <span class="o">*</span> <span class="n">m</span>
                    <span class="n">F</span> <span class="o">=</span> <span class="mi">3</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span> <span class="n">d</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span>
                    <span class="n">all_list</span> <span class="o">=</span> <span class="n">R_list_inv</span> <span class="o">+</span> <span class="n">R_list</span> <span class="o">+</span> <span class="p">[</span><span class="n">F</span><span class="p">]</span>
                    <span class="c1"># Compute the max common factors for the elements of the rotation matrix</span>
                    <span class="c1"># and its inverse.</span>
                    <span class="n">com_fac</span> <span class="o">=</span> <span class="n">reduce</span><span class="p">(</span><span class="n">gcd</span><span class="p">,</span> <span class="n">all_list</span><span class="p">)</span>
                    <span class="n">sigma</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="nb">round</span><span class="p">((</span><span class="mi">3</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span> <span class="n">d</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span><span class="p">)</span> <span class="o">/</span> <span class="n">com_fac</span><span class="p">))</span>
                    <span class="k">if</span> <span class="p">(</span><span class="n">sigma</span> <span class="o">&lt;=</span> <span class="n">cutoff</span><span class="p">)</span> <span class="ow">and</span> <span class="p">(</span><span class="n">sigma</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">):</span>
                        <span class="k">if</span> <span class="n">sigma</span> <span class="ow">not</span> <span class="ow">in</span> <span class="nb">list</span><span class="p">(</span><span class="n">sigmas</span><span class="o">.</span><span class="n">keys</span><span class="p">()):</span>
                            <span class="k">if</span> <span class="n">m</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                                <span class="n">angle</span> <span class="o">=</span> <span class="mf">180.0</span>
                            <span class="k">else</span><span class="p">:</span>
                                <span class="n">angle</span> <span class="o">=</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">arctan</span><span class="p">(</span><span class="n">n</span> <span class="o">/</span> <span class="n">m</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">d</span> <span class="o">/</span> <span class="mf">3.0</span> <span class="o">/</span> <span class="n">mu</span><span class="p">))</span> \
                                        <span class="o">/</span> <span class="n">np</span><span class="o">.</span><span class="n">pi</span> <span class="o">*</span> <span class="mi">180</span>
                            <span class="n">sigmas</span><span class="p">[</span><span class="n">sigma</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="n">angle</span><span class="p">]</span>
                        <span class="k">else</span><span class="p">:</span>
                            <span class="k">if</span> <span class="n">m</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                                <span class="n">angle</span> <span class="o">=</span> <span class="mf">180.0</span>
                            <span class="k">else</span><span class="p">:</span>
                                <span class="n">angle</span> <span class="o">=</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">arctan</span><span class="p">(</span><span class="n">n</span> <span class="o">/</span> <span class="n">m</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">d</span> <span class="o">/</span> <span class="mf">3.0</span> <span class="o">/</span> <span class="n">mu</span><span class="p">))</span> \
                                        <span class="o">/</span> <span class="n">np</span><span class="o">.</span><span class="n">pi</span> <span class="o">*</span> <span class="mi">180</span>
                            <span class="k">if</span> <span class="n">angle</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">sigmas</span><span class="p">[</span><span class="n">sigma</span><span class="p">]:</span>
                                <span class="n">sigmas</span><span class="p">[</span><span class="n">sigma</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">angle</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">m_max</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="k">break</span>
        <span class="k">return</span> <span class="n">sigmas</span></div>

<div class="viewcode-block" id="GrainBoundaryGenerator.enum_sigma_rho"><a class="viewcode-back" href="../../../../pymatgen.analysis.gb.grain.html#pymatgen.analysis.gb.grain.GrainBoundaryGenerator.enum_sigma_rho">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">enum_sigma_rho</span><span class="p">(</span><span class="n">cutoff</span><span class="p">,</span> <span class="n">r_axis</span><span class="p">,</span> <span class="n">ratio_alpha</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Find all possible sigma values and corresponding rotation angles</span>
<span class="sd">        within a sigma value cutoff with known rotation axis in rhombohedral system.</span>
<span class="sd">        The algorithm for this code is from reference, Acta Cryst, A45,505(1989).</span>

<span class="sd">        Args:</span>
<span class="sd">            cutoff (integer): the cutoff of sigma values.</span>
<span class="sd">            r_axis (list of three integers, e.g. u, v, w</span>
<span class="sd">                    or four integers, e.g. u, v, t, w):</span>
<span class="sd">                    the rotation axis of the grain boundary, with the format of [u,v,w]</span>
<span class="sd">                    or Weber indices [u, v, t, w].</span>
<span class="sd">            ratio_alpha (list of two integers, e.g. mu, mv):</span>
<span class="sd">                    mu/mv is the ratio of (1+2*cos(alpha))/cos(alpha) with rational number.</span>
<span class="sd">                    If irrational, set ratio_alpha = None.</span>
<span class="sd">        Returns:</span>
<span class="sd">            sigmas (dict):</span>
<span class="sd">                    dictionary with keys as the possible integer sigma values</span>
<span class="sd">                    and values as list of the possible rotation angles to the</span>
<span class="sd">                    corresponding sigma values.</span>
<span class="sd">                    e.g. the format as</span>
<span class="sd">                    {sigma1: [angle11,angle12,...], sigma2: [angle21, angle22,...],...}</span>
<span class="sd">                    Note: the angles are the rotation angle of one grain respect to the</span>
<span class="sd">                    other grain.</span>
<span class="sd">                    When generate the microstructure of the grain boundary using these</span>
<span class="sd">                    angles, you need to analyze the symmetry of the structure. Different</span>
<span class="sd">                    angles may result in equivalent microstructures.</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">sigmas</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="c1"># transform four index notation to three index notation</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">r_axis</span><span class="p">)</span> <span class="o">==</span> <span class="mi">4</span><span class="p">:</span>
            <span class="n">u1</span> <span class="o">=</span> <span class="n">r_axis</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
            <span class="n">v1</span> <span class="o">=</span> <span class="n">r_axis</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
            <span class="n">w1</span> <span class="o">=</span> <span class="n">r_axis</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span>
            <span class="n">u</span> <span class="o">=</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">u1</span> <span class="o">+</span> <span class="n">v1</span> <span class="o">+</span> <span class="n">w1</span>
            <span class="n">v</span> <span class="o">=</span> <span class="n">v1</span> <span class="o">+</span> <span class="n">w1</span> <span class="o">-</span> <span class="n">u1</span>
            <span class="n">w</span> <span class="o">=</span> <span class="n">w1</span> <span class="o">-</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">v1</span> <span class="o">-</span> <span class="n">u1</span>
            <span class="n">r_axis</span> <span class="o">=</span> <span class="p">[</span><span class="n">u</span><span class="p">,</span> <span class="n">v</span><span class="p">,</span> <span class="n">w</span><span class="p">]</span>
        <span class="c1"># make sure gcd(r_axis)==1</span>
        <span class="k">if</span> <span class="n">reduce</span><span class="p">(</span><span class="n">gcd</span><span class="p">,</span> <span class="n">r_axis</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">1</span><span class="p">:</span>
            <span class="n">r_axis</span> <span class="o">=</span> <span class="p">[</span><span class="nb">int</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">x</span> <span class="o">/</span> <span class="n">reduce</span><span class="p">(</span><span class="n">gcd</span><span class="p">,</span> <span class="n">r_axis</span><span class="p">)))</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">r_axis</span><span class="p">]</span>
        <span class="n">u</span><span class="p">,</span> <span class="n">v</span><span class="p">,</span> <span class="n">w</span> <span class="o">=</span> <span class="n">r_axis</span>
        <span class="c1"># make sure mu, mv are coprime integers.</span>
        <span class="k">if</span> <span class="n">ratio_alpha</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">mu</span><span class="p">,</span> <span class="n">mv</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">]</span>
            <span class="k">if</span> <span class="n">u</span> <span class="o">+</span> <span class="n">v</span> <span class="o">+</span> <span class="n">w</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">u</span> <span class="o">!=</span> <span class="n">v</span> <span class="ow">or</span> <span class="n">u</span> <span class="o">!=</span> <span class="n">w</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s1">&#39;For irrational ratio_alpha, CSL only exist for [1,1,1]&#39;</span>
                                       <span class="s1">&#39;or [u, v, -(u+v)] and m =0&#39;</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">mu</span><span class="p">,</span> <span class="n">mv</span> <span class="o">=</span> <span class="n">ratio_alpha</span>
            <span class="k">if</span> <span class="n">gcd</span><span class="p">(</span><span class="n">mu</span><span class="p">,</span> <span class="n">mv</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">1</span><span class="p">:</span>
                <span class="n">temp</span> <span class="o">=</span> <span class="n">gcd</span><span class="p">(</span><span class="n">mu</span><span class="p">,</span> <span class="n">mv</span><span class="p">)</span>
                <span class="n">mu</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">mu</span> <span class="o">/</span> <span class="n">temp</span><span class="p">))</span>
                <span class="n">mv</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">mv</span> <span class="o">/</span> <span class="n">temp</span><span class="p">))</span>

        <span class="c1"># refer to the meaning of d in reference</span>
        <span class="n">d</span> <span class="o">=</span> <span class="p">(</span><span class="n">u</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span> <span class="n">v</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span> <span class="n">w</span> <span class="o">**</span> <span class="mi">2</span><span class="p">)</span> <span class="o">*</span> <span class="p">(</span><span class="n">mu</span> <span class="o">-</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span><span class="p">)</span> <span class="o">+</span> \
            <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="p">(</span><span class="n">v</span> <span class="o">*</span> <span class="n">w</span> <span class="o">+</span> <span class="n">w</span> <span class="o">*</span> <span class="n">u</span> <span class="o">+</span> <span class="n">u</span> <span class="o">*</span> <span class="n">v</span><span class="p">)</span>
        <span class="c1"># Compute the max n we need to enumerate.</span>
        <span class="n">n_max</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">((</span><span class="n">cutoff</span> <span class="o">*</span> <span class="nb">abs</span><span class="p">(</span><span class="mi">4</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="p">(</span><span class="n">mu</span> <span class="o">-</span> <span class="mi">3</span> <span class="o">*</span> <span class="n">mv</span><span class="p">)))</span> <span class="o">/</span> <span class="nb">abs</span><span class="p">(</span><span class="n">d</span><span class="p">)))</span>

        <span class="c1"># Enumerate all possible n, m to give possible sigmas within the cutoff.</span>
        <span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">n_max</span> <span class="o">+</span> <span class="mi">1</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">ratio_alpha</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">u</span> <span class="o">+</span> <span class="n">v</span> <span class="o">+</span> <span class="n">w</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">m_max</span> <span class="o">=</span> <span class="mi">0</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">m_max</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">((</span><span class="n">cutoff</span> <span class="o">*</span> <span class="nb">abs</span><span class="p">(</span><span class="mi">4</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="p">(</span><span class="n">mu</span> <span class="o">-</span> <span class="mi">3</span> <span class="o">*</span> <span class="n">mv</span><span class="p">))</span> <span class="o">-</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">d</span><span class="p">)</span> <span class="o">/</span> <span class="p">(</span><span class="n">mu</span><span class="p">)))</span>
            <span class="k">for</span> <span class="n">m</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">m_max</span> <span class="o">+</span> <span class="mi">1</span><span class="p">):</span>
                <span class="k">if</span> <span class="n">gcd</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span> <span class="ow">or</span> <span class="n">m</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                    <span class="c1"># construct the rotation matrix, refer to the reference</span>
                    <span class="n">R_list</span> <span class="o">=</span> <span class="p">[(</span><span class="n">mu</span> <span class="o">-</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span><span class="p">)</span> <span class="o">*</span> <span class="p">(</span><span class="n">u</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">-</span> <span class="n">v</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">-</span> <span class="n">w</span> <span class="o">**</span> <span class="mi">2</span><span class="p">)</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span>
                              <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="p">(</span><span class="n">v</span> <span class="o">-</span> <span class="n">w</span><span class="p">)</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span> <span class="o">-</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">v</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span>
                              <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">**</span> <span class="mi">2</span><span class="p">,</span>
                              <span class="mi">2</span> <span class="o">*</span> <span class="p">(</span><span class="n">mv</span> <span class="o">*</span> <span class="n">u</span> <span class="o">*</span> <span class="n">n</span> <span class="o">*</span> <span class="p">(</span><span class="n">w</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="n">u</span> <span class="o">*</span> <span class="n">n</span> <span class="o">-</span> <span class="n">m</span><span class="p">)</span> <span class="o">-</span> <span class="p">(</span><span class="n">mu</span> <span class="o">-</span> <span class="n">mv</span><span class="p">)</span> <span class="o">*</span>
                                   <span class="n">m</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="p">(</span><span class="n">mu</span> <span class="o">-</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span><span class="p">)</span> <span class="o">*</span> <span class="n">u</span> <span class="o">*</span> <span class="n">v</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span><span class="p">),</span>
                              <span class="mi">2</span> <span class="o">*</span> <span class="p">(</span><span class="n">mv</span> <span class="o">*</span> <span class="n">u</span> <span class="o">*</span> <span class="n">n</span> <span class="o">*</span> <span class="p">(</span><span class="n">v</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="n">u</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="n">m</span><span class="p">)</span> <span class="o">+</span> <span class="p">(</span><span class="n">mu</span> <span class="o">-</span> <span class="n">mv</span><span class="p">)</span> <span class="o">*</span>
                                   <span class="n">m</span> <span class="o">*</span> <span class="n">v</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="p">(</span><span class="n">mu</span> <span class="o">-</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span><span class="p">)</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">u</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span><span class="p">),</span>
                              <span class="mi">2</span> <span class="o">*</span> <span class="p">(</span><span class="n">mv</span> <span class="o">*</span> <span class="n">v</span> <span class="o">*</span> <span class="n">n</span> <span class="o">*</span> <span class="p">(</span><span class="n">w</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="n">v</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="n">m</span><span class="p">)</span> <span class="o">+</span> <span class="p">(</span><span class="n">mu</span> <span class="o">-</span> <span class="n">mv</span><span class="p">)</span> <span class="o">*</span>
                                   <span class="n">m</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="p">(</span><span class="n">mu</span> <span class="o">-</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span><span class="p">)</span> <span class="o">*</span> <span class="n">u</span> <span class="o">*</span> <span class="n">v</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span><span class="p">),</span>
                              <span class="p">(</span><span class="n">mu</span> <span class="o">-</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span><span class="p">)</span> <span class="o">*</span> <span class="p">(</span><span class="n">v</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">-</span> <span class="n">w</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">-</span> <span class="n">u</span> <span class="o">**</span> <span class="mi">2</span><span class="p">)</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span>
                              <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="p">(</span><span class="n">w</span> <span class="o">-</span> <span class="n">u</span><span class="p">)</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span> <span class="o">-</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">u</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span>
                              <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">**</span> <span class="mi">2</span><span class="p">,</span>
                              <span class="mi">2</span> <span class="o">*</span> <span class="p">(</span><span class="n">mv</span> <span class="o">*</span> <span class="n">v</span> <span class="o">*</span> <span class="n">n</span> <span class="o">*</span> <span class="p">(</span><span class="n">v</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="n">u</span> <span class="o">*</span> <span class="n">n</span> <span class="o">-</span> <span class="n">m</span><span class="p">)</span> <span class="o">-</span> <span class="p">(</span><span class="n">mu</span> <span class="o">-</span> <span class="n">mv</span><span class="p">)</span> <span class="o">*</span>
                                   <span class="n">m</span> <span class="o">*</span> <span class="n">u</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="p">(</span><span class="n">mu</span> <span class="o">-</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span><span class="p">)</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">v</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span><span class="p">),</span>
                              <span class="mi">2</span> <span class="o">*</span> <span class="p">(</span><span class="n">mv</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">n</span> <span class="o">*</span> <span class="p">(</span><span class="n">w</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="n">v</span> <span class="o">*</span> <span class="n">n</span> <span class="o">-</span> <span class="n">m</span><span class="p">)</span> <span class="o">-</span> <span class="p">(</span><span class="n">mu</span> <span class="o">-</span> <span class="n">mv</span><span class="p">)</span> <span class="o">*</span>
                                   <span class="n">m</span> <span class="o">*</span> <span class="n">v</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="p">(</span><span class="n">mu</span> <span class="o">-</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span><span class="p">)</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">u</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span><span class="p">),</span>
                              <span class="mi">2</span> <span class="o">*</span> <span class="p">(</span><span class="n">mv</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">n</span> <span class="o">*</span> <span class="p">(</span><span class="n">w</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="n">u</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="n">m</span><span class="p">)</span> <span class="o">+</span> <span class="p">(</span><span class="n">mu</span> <span class="o">-</span> <span class="n">mv</span><span class="p">)</span> <span class="o">*</span>
                                   <span class="n">m</span> <span class="o">*</span> <span class="n">u</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="p">(</span><span class="n">mu</span> <span class="o">-</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span><span class="p">)</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">v</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span><span class="p">),</span>
                              <span class="p">(</span><span class="n">mu</span> <span class="o">-</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span><span class="p">)</span> <span class="o">*</span> <span class="p">(</span><span class="n">w</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">-</span> <span class="n">u</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">-</span> <span class="n">v</span> <span class="o">**</span> <span class="mi">2</span><span class="p">)</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span>
                              <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="p">(</span><span class="n">u</span> <span class="o">-</span> <span class="n">v</span><span class="p">)</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span> <span class="o">-</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">u</span> <span class="o">*</span> <span class="n">v</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span>
                              <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">**</span> <span class="mi">2</span><span class="p">]</span>
                    <span class="n">m</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span> <span class="o">*</span> <span class="n">m</span>
                    <span class="c1"># inverse of the rotation matrix</span>
                    <span class="n">R_list_inv</span> <span class="o">=</span> <span class="p">[(</span><span class="n">mu</span> <span class="o">-</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span><span class="p">)</span> <span class="o">*</span> <span class="p">(</span><span class="n">u</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">-</span> <span class="n">v</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">-</span> <span class="n">w</span> <span class="o">**</span> <span class="mi">2</span><span class="p">)</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span>
                                  <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="p">(</span><span class="n">v</span> <span class="o">-</span> <span class="n">w</span><span class="p">)</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span> <span class="o">-</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">v</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span>
                                  <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">**</span> <span class="mi">2</span><span class="p">,</span>
                                  <span class="mi">2</span> <span class="o">*</span> <span class="p">(</span><span class="n">mv</span> <span class="o">*</span> <span class="n">u</span> <span class="o">*</span> <span class="n">n</span> <span class="o">*</span> <span class="p">(</span><span class="n">w</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="n">u</span> <span class="o">*</span> <span class="n">n</span> <span class="o">-</span> <span class="n">m</span><span class="p">)</span> <span class="o">-</span> <span class="p">(</span><span class="n">mu</span> <span class="o">-</span> <span class="n">mv</span><span class="p">)</span> <span class="o">*</span>
                                       <span class="n">m</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="p">(</span><span class="n">mu</span> <span class="o">-</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span><span class="p">)</span> <span class="o">*</span> <span class="n">u</span> <span class="o">*</span> <span class="n">v</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span><span class="p">),</span>
                                  <span class="mi">2</span> <span class="o">*</span> <span class="p">(</span><span class="n">mv</span> <span class="o">*</span> <span class="n">u</span> <span class="o">*</span> <span class="n">n</span> <span class="o">*</span> <span class="p">(</span><span class="n">v</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="n">u</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="n">m</span><span class="p">)</span> <span class="o">+</span> <span class="p">(</span><span class="n">mu</span> <span class="o">-</span> <span class="n">mv</span><span class="p">)</span> <span class="o">*</span>
                                       <span class="n">m</span> <span class="o">*</span> <span class="n">v</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="p">(</span><span class="n">mu</span> <span class="o">-</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span><span class="p">)</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">u</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span><span class="p">),</span>
                                  <span class="mi">2</span> <span class="o">*</span> <span class="p">(</span><span class="n">mv</span> <span class="o">*</span> <span class="n">v</span> <span class="o">*</span> <span class="n">n</span> <span class="o">*</span> <span class="p">(</span><span class="n">w</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="n">v</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="n">m</span><span class="p">)</span> <span class="o">+</span> <span class="p">(</span><span class="n">mu</span> <span class="o">-</span> <span class="n">mv</span><span class="p">)</span> <span class="o">*</span>
                                       <span class="n">m</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="p">(</span><span class="n">mu</span> <span class="o">-</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span><span class="p">)</span> <span class="o">*</span> <span class="n">u</span> <span class="o">*</span> <span class="n">v</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span><span class="p">),</span>
                                  <span class="p">(</span><span class="n">mu</span> <span class="o">-</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span><span class="p">)</span> <span class="o">*</span> <span class="p">(</span><span class="n">v</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">-</span> <span class="n">w</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">-</span> <span class="n">u</span> <span class="o">**</span> <span class="mi">2</span><span class="p">)</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span>
                                  <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="p">(</span><span class="n">w</span> <span class="o">-</span> <span class="n">u</span><span class="p">)</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span> <span class="o">-</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">u</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span>
                                  <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">**</span> <span class="mi">2</span><span class="p">,</span>
                                  <span class="mi">2</span> <span class="o">*</span> <span class="p">(</span><span class="n">mv</span> <span class="o">*</span> <span class="n">v</span> <span class="o">*</span> <span class="n">n</span> <span class="o">*</span> <span class="p">(</span><span class="n">v</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="n">u</span> <span class="o">*</span> <span class="n">n</span> <span class="o">-</span> <span class="n">m</span><span class="p">)</span> <span class="o">-</span> <span class="p">(</span><span class="n">mu</span> <span class="o">-</span> <span class="n">mv</span><span class="p">)</span> <span class="o">*</span>
                                       <span class="n">m</span> <span class="o">*</span> <span class="n">u</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="p">(</span><span class="n">mu</span> <span class="o">-</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span><span class="p">)</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">v</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span><span class="p">),</span>
                                  <span class="mi">2</span> <span class="o">*</span> <span class="p">(</span><span class="n">mv</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">n</span> <span class="o">*</span> <span class="p">(</span><span class="n">w</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="n">v</span> <span class="o">*</span> <span class="n">n</span> <span class="o">-</span> <span class="n">m</span><span class="p">)</span> <span class="o">-</span> <span class="p">(</span><span class="n">mu</span> <span class="o">-</span> <span class="n">mv</span><span class="p">)</span> <span class="o">*</span>
                                       <span class="n">m</span> <span class="o">*</span> <span class="n">v</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="p">(</span><span class="n">mu</span> <span class="o">-</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span><span class="p">)</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">u</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span><span class="p">),</span>
                                  <span class="mi">2</span> <span class="o">*</span> <span class="p">(</span><span class="n">mv</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">n</span> <span class="o">*</span> <span class="p">(</span><span class="n">w</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="n">u</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="n">m</span><span class="p">)</span> <span class="o">+</span> <span class="p">(</span><span class="n">mu</span> <span class="o">-</span> <span class="n">mv</span><span class="p">)</span> <span class="o">*</span>
                                       <span class="n">m</span> <span class="o">*</span> <span class="n">u</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="p">(</span><span class="n">mu</span> <span class="o">-</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span><span class="p">)</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">v</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span><span class="p">),</span>
                                  <span class="p">(</span><span class="n">mu</span> <span class="o">-</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span><span class="p">)</span> <span class="o">*</span> <span class="p">(</span><span class="n">w</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">-</span> <span class="n">u</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">-</span> <span class="n">v</span> <span class="o">**</span> <span class="mi">2</span><span class="p">)</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span>
                                  <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="p">(</span><span class="n">u</span> <span class="o">-</span> <span class="n">v</span><span class="p">)</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span> <span class="o">-</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">u</span> <span class="o">*</span> <span class="n">v</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span>
                                  <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">**</span> <span class="mi">2</span><span class="p">]</span>
                    <span class="n">m</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span> <span class="o">*</span> <span class="n">m</span>
                    <span class="n">F</span> <span class="o">=</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span> <span class="n">d</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span>
                    <span class="n">all_list</span> <span class="o">=</span> <span class="n">R_list_inv</span> <span class="o">+</span> <span class="n">R_list</span> <span class="o">+</span> <span class="p">[</span><span class="n">F</span><span class="p">]</span>
                    <span class="c1"># Compute the max common factors for the elements of the rotation matrix</span>
                    <span class="c1">#  and its inverse.</span>
                    <span class="n">com_fac</span> <span class="o">=</span> <span class="n">reduce</span><span class="p">(</span><span class="n">gcd</span><span class="p">,</span> <span class="n">all_list</span><span class="p">)</span>
                    <span class="n">sigma</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="nb">abs</span><span class="p">(</span><span class="n">F</span> <span class="o">/</span> <span class="n">com_fac</span><span class="p">)))</span>
                    <span class="k">if</span> <span class="p">(</span><span class="n">sigma</span> <span class="o">&lt;=</span> <span class="n">cutoff</span><span class="p">)</span> <span class="ow">and</span> <span class="p">(</span><span class="n">sigma</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">):</span>
                        <span class="k">if</span> <span class="n">sigma</span> <span class="ow">not</span> <span class="ow">in</span> <span class="nb">list</span><span class="p">(</span><span class="n">sigmas</span><span class="o">.</span><span class="n">keys</span><span class="p">()):</span>
                            <span class="k">if</span> <span class="n">m</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                                <span class="n">angle</span> <span class="o">=</span> <span class="mf">180.0</span>
                            <span class="k">else</span><span class="p">:</span>
                                <span class="n">angle</span> <span class="o">=</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">arctan</span><span class="p">(</span><span class="n">n</span> <span class="o">/</span> <span class="n">m</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">d</span> <span class="o">/</span> <span class="n">mu</span><span class="p">))</span> \
                                        <span class="o">/</span> <span class="n">np</span><span class="o">.</span><span class="n">pi</span> <span class="o">*</span> <span class="mi">180</span>
                            <span class="n">sigmas</span><span class="p">[</span><span class="n">sigma</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="n">angle</span><span class="p">]</span>
                        <span class="k">else</span><span class="p">:</span>
                            <span class="k">if</span> <span class="n">m</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                                <span class="n">angle</span> <span class="o">=</span> <span class="mi">180</span>
                            <span class="k">else</span><span class="p">:</span>
                                <span class="n">angle</span> <span class="o">=</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">arctan</span><span class="p">(</span><span class="n">n</span> <span class="o">/</span> <span class="n">m</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">d</span> <span class="o">/</span> <span class="n">mu</span><span class="p">))</span> \
                                        <span class="o">/</span> <span class="n">np</span><span class="o">.</span><span class="n">pi</span> <span class="o">*</span> <span class="mf">180.0</span>
                            <span class="k">if</span> <span class="n">angle</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">sigmas</span><span class="p">[</span><span class="n">sigma</span><span class="p">]:</span>
                                <span class="n">sigmas</span><span class="p">[</span><span class="n">sigma</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">angle</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">m_max</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="k">break</span>
        <span class="k">return</span> <span class="n">sigmas</span></div>

<div class="viewcode-block" id="GrainBoundaryGenerator.enum_sigma_tet"><a class="viewcode-back" href="../../../../pymatgen.analysis.gb.grain.html#pymatgen.analysis.gb.grain.GrainBoundaryGenerator.enum_sigma_tet">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">enum_sigma_tet</span><span class="p">(</span><span class="n">cutoff</span><span class="p">,</span> <span class="n">r_axis</span><span class="p">,</span> <span class="n">c2_a2_ratio</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Find all possible sigma values and corresponding rotation angles</span>
<span class="sd">        within a sigma value cutoff with known rotation axis in tetragonal system.</span>
<span class="sd">        The algorithm for this code is from reference, Acta Cryst, B46,117(1990)</span>

<span class="sd">        Args:</span>
<span class="sd">            cutoff (integer): the cutoff of sigma values.</span>
<span class="sd">            r_axis (list of three integers, e.g. u, v, w):</span>
<span class="sd">                    the rotation axis of the grain boundary, with the format of [u,v,w].</span>
<span class="sd">            c2_a2_ratio (list of two integers, e.g. mu, mv):</span>
<span class="sd">                    mu/mv is the square of the tetragonal axial ratio with rational number.</span>
<span class="sd">                    if irrational, set c2_a2_ratio = None</span>
<span class="sd">        Returns:</span>
<span class="sd">            sigmas (dict):</span>
<span class="sd">                    dictionary with keys as the possible integer sigma values</span>
<span class="sd">                    and values as list of the possible rotation angles to the</span>
<span class="sd">                    corresponding sigma values.</span>
<span class="sd">                    e.g. the format as</span>
<span class="sd">                    {sigma1: [angle11,angle12,...], sigma2: [angle21, angle22,...],...}</span>
<span class="sd">                    Note: the angles are the rotation angle of one grain respect to the</span>
<span class="sd">                    other grain.</span>
<span class="sd">                    When generate the microstructure of the grain boundary using these</span>
<span class="sd">                    angles, you need to analyze the symmetry of the structure. Different</span>
<span class="sd">                    angles may result in equivalent microstructures.</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">sigmas</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="c1"># make sure gcd(r_axis)==1</span>
        <span class="k">if</span> <span class="n">reduce</span><span class="p">(</span><span class="n">gcd</span><span class="p">,</span> <span class="n">r_axis</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">1</span><span class="p">:</span>
            <span class="n">r_axis</span> <span class="o">=</span> <span class="p">[</span><span class="nb">int</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">x</span> <span class="o">/</span> <span class="n">reduce</span><span class="p">(</span><span class="n">gcd</span><span class="p">,</span> <span class="n">r_axis</span><span class="p">)))</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">r_axis</span><span class="p">]</span>

        <span class="n">u</span><span class="p">,</span> <span class="n">v</span><span class="p">,</span> <span class="n">w</span> <span class="o">=</span> <span class="n">r_axis</span>

        <span class="c1"># make sure mu, mv are coprime integers.</span>
        <span class="k">if</span> <span class="n">c2_a2_ratio</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">mu</span><span class="p">,</span> <span class="n">mv</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">]</span>
            <span class="k">if</span> <span class="n">w</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">u</span> <span class="o">!=</span> <span class="mi">0</span> <span class="ow">or</span> <span class="p">(</span><span class="n">v</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">):</span>
                    <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s1">&#39;For irrational c2/a2, CSL only exist for [0,0,1] &#39;</span>
                                       <span class="s1">&#39;or [u,v,0] and m = 0&#39;</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">mu</span><span class="p">,</span> <span class="n">mv</span> <span class="o">=</span> <span class="n">c2_a2_ratio</span>
            <span class="k">if</span> <span class="n">gcd</span><span class="p">(</span><span class="n">mu</span><span class="p">,</span> <span class="n">mv</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">1</span><span class="p">:</span>
                <span class="n">temp</span> <span class="o">=</span> <span class="n">gcd</span><span class="p">(</span><span class="n">mu</span><span class="p">,</span> <span class="n">mv</span><span class="p">)</span>
                <span class="n">mu</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">mu</span> <span class="o">/</span> <span class="n">temp</span><span class="p">))</span>
                <span class="n">mv</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">mv</span> <span class="o">/</span> <span class="n">temp</span><span class="p">))</span>

        <span class="c1"># refer to the meaning of d in reference</span>
        <span class="n">d</span> <span class="o">=</span> <span class="p">(</span><span class="n">u</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span> <span class="n">v</span> <span class="o">**</span> <span class="mi">2</span><span class="p">)</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">+</span> <span class="n">w</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mu</span>

        <span class="c1"># Compute the max n we need to enumerate.</span>
        <span class="n">n_max</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">((</span><span class="n">cutoff</span> <span class="o">*</span> <span class="mi">4</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">mv</span><span class="p">)</span> <span class="o">/</span> <span class="n">d</span><span class="p">))</span>

        <span class="c1"># Enumerate all possible n, m to give possible sigmas within the cutoff.</span>
        <span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">n_max</span> <span class="o">+</span> <span class="mi">1</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">c2_a2_ratio</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">w</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">m_max</span> <span class="o">=</span> <span class="mi">0</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">m_max</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">((</span><span class="n">cutoff</span> <span class="o">*</span> <span class="mi">4</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">-</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">d</span><span class="p">)</span> <span class="o">/</span> <span class="n">mu</span><span class="p">))</span>
            <span class="k">for</span> <span class="n">m</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">m_max</span> <span class="o">+</span> <span class="mi">1</span><span class="p">):</span>
                <span class="k">if</span> <span class="n">gcd</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span> <span class="ow">or</span> <span class="n">m</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                    <span class="c1"># construct the rotation matrix, refer to the reference</span>
                    <span class="n">R_list</span> <span class="o">=</span> <span class="p">[(</span><span class="n">u</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">-</span> <span class="n">v</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">-</span> <span class="n">w</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mu</span><span class="p">)</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span>
                              <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">**</span> <span class="mi">2</span><span class="p">,</span>
                              <span class="mi">2</span> <span class="o">*</span> <span class="n">v</span> <span class="o">*</span> <span class="n">u</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">-</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span><span class="p">,</span>
                              <span class="mi">2</span> <span class="o">*</span> <span class="n">u</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">v</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span><span class="p">,</span>
                              <span class="mi">2</span> <span class="o">*</span> <span class="n">u</span> <span class="o">*</span> <span class="n">v</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span><span class="p">,</span>
                              <span class="p">(</span><span class="n">v</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">-</span> <span class="n">u</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">-</span> <span class="n">w</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mu</span><span class="p">)</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span>
                              <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">**</span> <span class="mi">2</span><span class="p">,</span>
                              <span class="mi">2</span> <span class="o">*</span> <span class="n">v</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">-</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">u</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span><span class="p">,</span>
                              <span class="mi">2</span> <span class="o">*</span> <span class="n">u</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">-</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">v</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span><span class="p">,</span>
                              <span class="mi">2</span> <span class="o">*</span> <span class="n">v</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">u</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span><span class="p">,</span>
                              <span class="p">(</span><span class="n">w</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">-</span> <span class="n">u</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">-</span> <span class="n">v</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span><span class="p">)</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span>
                              <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">**</span> <span class="mi">2</span><span class="p">]</span>
                    <span class="n">m</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span> <span class="o">*</span> <span class="n">m</span>
                    <span class="c1"># inverse of rotation matrix</span>
                    <span class="n">R_list_inv</span> <span class="o">=</span> <span class="p">[(</span><span class="n">u</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">-</span> <span class="n">v</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">-</span> <span class="n">w</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mu</span><span class="p">)</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span>
                                  <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">**</span> <span class="mi">2</span><span class="p">,</span>
                                  <span class="mi">2</span> <span class="o">*</span> <span class="n">v</span> <span class="o">*</span> <span class="n">u</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">-</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span><span class="p">,</span>
                                  <span class="mi">2</span> <span class="o">*</span> <span class="n">u</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">v</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span><span class="p">,</span>
                                  <span class="mi">2</span> <span class="o">*</span> <span class="n">u</span> <span class="o">*</span> <span class="n">v</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span><span class="p">,</span>
                                  <span class="p">(</span><span class="n">v</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">-</span> <span class="n">u</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">-</span> <span class="n">w</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mu</span><span class="p">)</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span>
                                  <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">**</span> <span class="mi">2</span><span class="p">,</span>
                                  <span class="mi">2</span> <span class="o">*</span> <span class="n">v</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">-</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">u</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span><span class="p">,</span>
                                  <span class="mi">2</span> <span class="o">*</span> <span class="n">u</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">-</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">v</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span><span class="p">,</span>
                                  <span class="mi">2</span> <span class="o">*</span> <span class="n">v</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">u</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span><span class="p">,</span>
                                  <span class="p">(</span><span class="n">w</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">-</span> <span class="n">u</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">-</span> <span class="n">v</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span><span class="p">)</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span>
                                  <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">**</span> <span class="mi">2</span><span class="p">]</span>
                    <span class="n">m</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span> <span class="o">*</span> <span class="n">m</span>
                    <span class="n">F</span> <span class="o">=</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span> <span class="n">d</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span>
                    <span class="n">all_list</span> <span class="o">=</span> <span class="n">R_list</span> <span class="o">+</span> <span class="n">R_list_inv</span> <span class="o">+</span> <span class="p">[</span><span class="n">F</span><span class="p">]</span>
                    <span class="c1"># Compute the max common factors for the elements of the rotation matrix</span>
                    <span class="c1">#  and its inverse.</span>
                    <span class="n">com_fac</span> <span class="o">=</span> <span class="n">reduce</span><span class="p">(</span><span class="n">gcd</span><span class="p">,</span> <span class="n">all_list</span><span class="p">)</span>
                    <span class="n">sigma</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="nb">round</span><span class="p">((</span><span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span> <span class="n">d</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span><span class="p">)</span> <span class="o">/</span> <span class="n">com_fac</span><span class="p">))</span>
                    <span class="k">if</span> <span class="p">(</span><span class="n">sigma</span> <span class="o">&lt;=</span> <span class="n">cutoff</span><span class="p">)</span> <span class="ow">and</span> <span class="p">(</span><span class="n">sigma</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">):</span>
                        <span class="k">if</span> <span class="n">sigma</span> <span class="ow">not</span> <span class="ow">in</span> <span class="nb">list</span><span class="p">(</span><span class="n">sigmas</span><span class="o">.</span><span class="n">keys</span><span class="p">()):</span>
                            <span class="k">if</span> <span class="n">m</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                                <span class="n">angle</span> <span class="o">=</span> <span class="mf">180.0</span>
                            <span class="k">else</span><span class="p">:</span>
                                <span class="n">angle</span> <span class="o">=</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">arctan</span><span class="p">(</span><span class="n">n</span> <span class="o">/</span> <span class="n">m</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">d</span> <span class="o">/</span> <span class="n">mu</span><span class="p">))</span> \
                                        <span class="o">/</span> <span class="n">np</span><span class="o">.</span><span class="n">pi</span> <span class="o">*</span> <span class="mi">180</span>
                            <span class="n">sigmas</span><span class="p">[</span><span class="n">sigma</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="n">angle</span><span class="p">]</span>
                        <span class="k">else</span><span class="p">:</span>
                            <span class="k">if</span> <span class="n">m</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                                <span class="n">angle</span> <span class="o">=</span> <span class="mf">180.0</span>
                            <span class="k">else</span><span class="p">:</span>
                                <span class="n">angle</span> <span class="o">=</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">arctan</span><span class="p">(</span><span class="n">n</span> <span class="o">/</span> <span class="n">m</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">d</span> <span class="o">/</span> <span class="n">mu</span><span class="p">))</span> \
                                        <span class="o">/</span> <span class="n">np</span><span class="o">.</span><span class="n">pi</span> <span class="o">*</span> <span class="mi">180</span>
                            <span class="k">if</span> <span class="n">angle</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">sigmas</span><span class="p">[</span><span class="n">sigma</span><span class="p">]:</span>
                                <span class="n">sigmas</span><span class="p">[</span><span class="n">sigma</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">angle</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">m_max</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="k">break</span>

        <span class="k">return</span> <span class="n">sigmas</span></div>

<div class="viewcode-block" id="GrainBoundaryGenerator.enum_sigma_ort"><a class="viewcode-back" href="../../../../pymatgen.analysis.gb.grain.html#pymatgen.analysis.gb.grain.GrainBoundaryGenerator.enum_sigma_ort">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">enum_sigma_ort</span><span class="p">(</span><span class="n">cutoff</span><span class="p">,</span> <span class="n">r_axis</span><span class="p">,</span> <span class="n">c2_b2_a2_ratio</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Find all possible sigma values and corresponding rotation angles</span>
<span class="sd">        within a sigma value cutoff with known rotation axis in orthorhombic system.</span>
<span class="sd">        The algorithm for this code is from reference, Scipta Metallurgica 27, 291(1992)</span>

<span class="sd">        Args:</span>
<span class="sd">            cutoff (integer): the cutoff of sigma values.</span>
<span class="sd">            r_axis (list of three integers, e.g. u, v, w):</span>
<span class="sd">                    the rotation axis of the grain boundary, with the format of [u,v,w].</span>
<span class="sd">            c2_b2_a2_ratio (list of three integers, e.g. mu,lamda, mv):</span>
<span class="sd">                    mu:lam:mv is the square of the orthorhombic axial ratio with rational</span>
<span class="sd">                    numbers. If irrational for one axis, set it to None.</span>
<span class="sd">                    e.g. mu:lam:mv = c2,None,a2, means b2 is irrational.</span>
<span class="sd">        Returns:</span>
<span class="sd">            sigmas (dict):</span>
<span class="sd">                    dictionary with keys as the possible integer sigma values</span>
<span class="sd">                    and values as list of the possible rotation angles to the</span>
<span class="sd">                    corresponding sigma values.</span>
<span class="sd">                    e.g. the format as</span>
<span class="sd">                    {sigma1: [angle11,angle12,...], sigma2: [angle21, angle22,...],...}</span>
<span class="sd">                    Note: the angles are the rotation angle of one grain respect to the</span>
<span class="sd">                    other grain.</span>
<span class="sd">                    When generate the microstructure of the grain boundary using these</span>
<span class="sd">                    angles, you need to analyze the symmetry of the structure. Different</span>
<span class="sd">                    angles may result in equivalent microstructures.</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">sigmas</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="c1"># make sure gcd(r_axis)==1</span>
        <span class="k">if</span> <span class="n">reduce</span><span class="p">(</span><span class="n">gcd</span><span class="p">,</span> <span class="n">r_axis</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">1</span><span class="p">:</span>
            <span class="n">r_axis</span> <span class="o">=</span> <span class="p">[</span><span class="nb">int</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">x</span> <span class="o">/</span> <span class="n">reduce</span><span class="p">(</span><span class="n">gcd</span><span class="p">,</span> <span class="n">r_axis</span><span class="p">)))</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">r_axis</span><span class="p">]</span>

        <span class="n">u</span><span class="p">,</span> <span class="n">v</span><span class="p">,</span> <span class="n">w</span> <span class="o">=</span> <span class="n">r_axis</span>
        <span class="c1"># make sure mu, lambda, mv are coprime integers.</span>
        <span class="k">if</span> <span class="kc">None</span> <span class="ow">in</span> <span class="n">c2_b2_a2_ratio</span><span class="p">:</span>
            <span class="n">mu</span><span class="p">,</span> <span class="n">lam</span><span class="p">,</span> <span class="n">mv</span> <span class="o">=</span> <span class="n">c2_b2_a2_ratio</span>
            <span class="n">non_none</span> <span class="o">=</span> <span class="p">[</span><span class="n">i</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">c2_b2_a2_ratio</span> <span class="k">if</span> <span class="n">i</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">]</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">non_none</span><span class="p">)</span> <span class="o">&lt;</span> <span class="mi">2</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s1">&#39;No CSL exist for two irrational numbers&#39;</span><span class="p">)</span>
            <span class="n">non1</span><span class="p">,</span> <span class="n">non2</span> <span class="o">=</span> <span class="n">non_none</span>
            <span class="k">if</span> <span class="n">reduce</span><span class="p">(</span><span class="n">gcd</span><span class="p">,</span> <span class="n">non_none</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">1</span><span class="p">:</span>
                <span class="n">temp</span> <span class="o">=</span> <span class="n">reduce</span><span class="p">(</span><span class="n">gcd</span><span class="p">,</span> <span class="n">non_none</span><span class="p">)</span>
                <span class="n">non1</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">non1</span> <span class="o">/</span> <span class="n">temp</span><span class="p">))</span>
                <span class="n">non2</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">non2</span> <span class="o">/</span> <span class="n">temp</span><span class="p">))</span>
            <span class="k">if</span> <span class="n">mu</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">lam</span> <span class="o">=</span> <span class="n">non1</span>
                <span class="n">mv</span> <span class="o">=</span> <span class="n">non2</span>
                <span class="n">mu</span> <span class="o">=</span> <span class="mi">1</span>
                <span class="k">if</span> <span class="n">w</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">:</span>
                    <span class="k">if</span> <span class="n">u</span> <span class="o">!=</span> <span class="mi">0</span> <span class="ow">or</span> <span class="p">(</span><span class="n">v</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">):</span>
                        <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s1">&#39;For irrational c2, CSL only exist for [0,0,1] &#39;</span>
                                           <span class="s1">&#39;or [u,v,0] and m = 0&#39;</span><span class="p">)</span>
            <span class="k">elif</span> <span class="n">lam</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">mu</span> <span class="o">=</span> <span class="n">non1</span>
                <span class="n">mv</span> <span class="o">=</span> <span class="n">non2</span>
                <span class="n">lam</span> <span class="o">=</span> <span class="mi">1</span>
                <span class="k">if</span> <span class="n">v</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">:</span>
                    <span class="k">if</span> <span class="n">u</span> <span class="o">!=</span> <span class="mi">0</span> <span class="ow">or</span> <span class="p">(</span><span class="n">w</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">):</span>
                        <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s1">&#39;For irrational b2, CSL only exist for [0,1,0] &#39;</span>
                                           <span class="s1">&#39;or [u,0,w] and m = 0&#39;</span><span class="p">)</span>
            <span class="k">elif</span> <span class="n">mv</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">mu</span> <span class="o">=</span> <span class="n">non1</span>
                <span class="n">lam</span> <span class="o">=</span> <span class="n">non2</span>
                <span class="n">mv</span> <span class="o">=</span> <span class="mi">1</span>
                <span class="k">if</span> <span class="n">u</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">:</span>
                    <span class="k">if</span> <span class="n">w</span> <span class="o">!=</span> <span class="mi">0</span> <span class="ow">or</span> <span class="p">(</span><span class="n">v</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">):</span>
                        <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s1">&#39;For irrational a2, CSL only exist for [1,0,0] &#39;</span>
                                           <span class="s1">&#39;or [0,v,w] and m = 0&#39;</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">mu</span><span class="p">,</span> <span class="n">lam</span><span class="p">,</span> <span class="n">mv</span> <span class="o">=</span> <span class="n">c2_b2_a2_ratio</span>
            <span class="k">if</span> <span class="n">reduce</span><span class="p">(</span><span class="n">gcd</span><span class="p">,</span> <span class="n">c2_b2_a2_ratio</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">1</span><span class="p">:</span>
                <span class="n">temp</span> <span class="o">=</span> <span class="n">reduce</span><span class="p">(</span><span class="n">gcd</span><span class="p">,</span> <span class="n">c2_b2_a2_ratio</span><span class="p">)</span>
                <span class="n">mu</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">mu</span> <span class="o">/</span> <span class="n">temp</span><span class="p">))</span>
                <span class="n">mv</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">mv</span> <span class="o">/</span> <span class="n">temp</span><span class="p">))</span>
                <span class="n">lam</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">lam</span> <span class="o">/</span> <span class="n">temp</span><span class="p">))</span>
            <span class="k">if</span> <span class="n">u</span> <span class="o">==</span> <span class="mi">0</span> <span class="ow">and</span> <span class="n">v</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">mu</span> <span class="o">=</span> <span class="mi">1</span>
            <span class="k">if</span> <span class="n">u</span> <span class="o">==</span> <span class="mi">0</span> <span class="ow">and</span> <span class="n">w</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">lam</span> <span class="o">=</span> <span class="mi">1</span>
            <span class="k">if</span> <span class="n">v</span> <span class="o">==</span> <span class="mi">0</span> <span class="ow">and</span> <span class="n">w</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">mv</span> <span class="o">=</span> <span class="mi">1</span>
        <span class="c1"># refer to the meaning of d in reference</span>
        <span class="n">d</span> <span class="o">=</span> <span class="p">(</span><span class="n">mv</span> <span class="o">*</span> <span class="n">u</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span> <span class="n">lam</span> <span class="o">*</span> <span class="n">v</span> <span class="o">**</span> <span class="mi">2</span><span class="p">)</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">+</span> <span class="n">w</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">mv</span>

        <span class="c1"># Compute the max n we need to enumerate.</span>
        <span class="n">n_max</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">((</span><span class="n">cutoff</span> <span class="o">*</span> <span class="mi">4</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">lam</span><span class="p">)</span> <span class="o">/</span> <span class="n">d</span><span class="p">))</span>
        <span class="c1"># Enumerate all possible n, m to give possible sigmas within the cutoff.</span>
        <span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">n_max</span> <span class="o">+</span> <span class="mi">1</span><span class="p">):</span>
            <span class="n">mu_temp</span><span class="p">,</span> <span class="n">lam_temp</span><span class="p">,</span> <span class="n">mv_temp</span> <span class="o">=</span> <span class="n">c2_b2_a2_ratio</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">mu_temp</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">w</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span> <span class="ow">or</span> <span class="p">(</span><span class="n">lam_temp</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">v</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span> \
                    <span class="ow">or</span> <span class="p">(</span><span class="n">mv_temp</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">u</span> <span class="o">==</span> <span class="mi">0</span><span class="p">):</span>
                <span class="n">m_max</span> <span class="o">=</span> <span class="mi">0</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">m_max</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">((</span><span class="n">cutoff</span> <span class="o">*</span> <span class="mi">4</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">lam</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">-</span>
                                     <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">d</span><span class="p">)</span> <span class="o">/</span> <span class="n">mu</span> <span class="o">/</span> <span class="n">lam</span><span class="p">))</span>
            <span class="k">for</span> <span class="n">m</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">m_max</span> <span class="o">+</span> <span class="mi">1</span><span class="p">):</span>

                <span class="k">if</span> <span class="n">gcd</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span> <span class="ow">or</span> <span class="n">m</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                    <span class="c1"># construct the rotation matrix, refer to the reference</span>
                    <span class="n">R_list</span> <span class="o">=</span> <span class="p">[(</span><span class="n">u</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">-</span> <span class="n">lam</span> <span class="o">*</span> <span class="n">v</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">-</span>
                               <span class="n">w</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">mv</span><span class="p">)</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span> <span class="n">lam</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">**</span> <span class="mi">2</span><span class="p">,</span>
                              <span class="mi">2</span> <span class="o">*</span> <span class="n">lam</span> <span class="o">*</span> <span class="p">(</span><span class="n">v</span> <span class="o">*</span> <span class="n">u</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">-</span> <span class="n">w</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span><span class="p">),</span>
                              <span class="mi">2</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="p">(</span><span class="n">u</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span> <span class="n">v</span> <span class="o">*</span> <span class="n">lam</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span><span class="p">),</span>
                              <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="p">(</span><span class="n">u</span> <span class="o">*</span> <span class="n">v</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span> <span class="n">w</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span><span class="p">),</span>
                              <span class="p">(</span><span class="n">v</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">lam</span> <span class="o">-</span> <span class="n">u</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">-</span>
                               <span class="n">w</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">mv</span><span class="p">)</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span> <span class="n">lam</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">**</span> <span class="mi">2</span><span class="p">,</span>
                              <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="p">(</span><span class="n">v</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">-</span> <span class="n">u</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span><span class="p">),</span>
                              <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="p">(</span><span class="n">u</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">-</span> <span class="n">v</span> <span class="o">*</span> <span class="n">lam</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span><span class="p">),</span>
                              <span class="mi">2</span> <span class="o">*</span> <span class="n">lam</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="p">(</span><span class="n">v</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span> <span class="n">u</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span><span class="p">),</span>
                              <span class="p">(</span><span class="n">w</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">-</span> <span class="n">u</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">-</span>
                               <span class="n">v</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">lam</span><span class="p">)</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span> <span class="n">lam</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">**</span> <span class="mi">2</span><span class="p">]</span>
                    <span class="n">m</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span> <span class="o">*</span> <span class="n">m</span>
                    <span class="c1"># inverse of rotation matrix</span>
                    <span class="n">R_list_inv</span> <span class="o">=</span> <span class="p">[(</span><span class="n">u</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">-</span> <span class="n">lam</span> <span class="o">*</span> <span class="n">v</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">-</span>
                                   <span class="n">w</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">mv</span><span class="p">)</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span> <span class="n">lam</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">**</span> <span class="mi">2</span><span class="p">,</span>
                                  <span class="mi">2</span> <span class="o">*</span> <span class="n">lam</span> <span class="o">*</span> <span class="p">(</span><span class="n">v</span> <span class="o">*</span> <span class="n">u</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">-</span> <span class="n">w</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span><span class="p">),</span>
                                  <span class="mi">2</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="p">(</span><span class="n">u</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span> <span class="n">v</span> <span class="o">*</span> <span class="n">lam</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span><span class="p">),</span>
                                  <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="p">(</span><span class="n">u</span> <span class="o">*</span> <span class="n">v</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span> <span class="n">w</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span><span class="p">),</span>
                                  <span class="p">(</span><span class="n">v</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">lam</span> <span class="o">-</span> <span class="n">u</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">-</span>
                                   <span class="n">w</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">mv</span><span class="p">)</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span> <span class="n">lam</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">**</span> <span class="mi">2</span><span class="p">,</span>
                                  <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="p">(</span><span class="n">v</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">-</span> <span class="n">u</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span><span class="p">),</span>
                                  <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="p">(</span><span class="n">u</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">-</span> <span class="n">v</span> <span class="o">*</span> <span class="n">lam</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span><span class="p">),</span>
                                  <span class="mi">2</span> <span class="o">*</span> <span class="n">lam</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="p">(</span><span class="n">v</span> <span class="o">*</span> <span class="n">w</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span> <span class="n">u</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">n</span><span class="p">),</span>
                                  <span class="p">(</span><span class="n">w</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">-</span> <span class="n">u</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">-</span>
                                   <span class="n">v</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">mv</span> <span class="o">*</span> <span class="n">lam</span><span class="p">)</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span> <span class="n">lam</span> <span class="o">*</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">m</span> <span class="o">**</span> <span class="mi">2</span><span class="p">]</span>
                    <span class="n">m</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span> <span class="o">*</span> <span class="n">m</span>
                    <span class="n">F</span> <span class="o">=</span> <span class="n">mu</span> <span class="o">*</span> <span class="n">lam</span> <span class="o">*</span> <span class="n">m</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span> <span class="n">d</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span>
                    <span class="n">all_list</span> <span class="o">=</span> <span class="n">R_list</span> <span class="o">+</span> <span class="n">R_list_inv</span> <span class="o">+</span> <span class="p">[</span><span class="n">F</span><span class="p">]</span>
                    <span class="c1"># Compute the max common factors for the elements of the rotation matrix</span>
                    <span class="c1">#  and its inverse.</span>
                    <span class="n">com_fac</span> <span class="o">=</span> <span class="n">reduce</span><span class="p">(</span><span class="n">gcd</span><span class="p">,</span> <span class="n">all_list</span><span class="p">)</span>
                    <span class="n">sigma</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="nb">round</span><span class="p">((</span><span class="n">mu</span> <span class="o">*</span> <span class="n">lam</span> <span class="o">*</span> <span class="n">m</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span> <span class="n">d</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span><span class="p">)</span> <span class="o">/</span> <span class="n">com_fac</span><span class="p">))</span>
                    <span class="k">if</span> <span class="p">(</span><span class="n">sigma</span> <span class="o">&lt;=</span> <span class="n">cutoff</span><span class="p">)</span> <span class="ow">and</span> <span class="p">(</span><span class="n">sigma</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">):</span>
                        <span class="k">if</span> <span class="n">sigma</span> <span class="ow">not</span> <span class="ow">in</span> <span class="nb">list</span><span class="p">(</span><span class="n">sigmas</span><span class="o">.</span><span class="n">keys</span><span class="p">()):</span>
                            <span class="k">if</span> <span class="n">m</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                                <span class="n">angle</span> <span class="o">=</span> <span class="mf">180.0</span>
                            <span class="k">else</span><span class="p">:</span>
                                <span class="n">angle</span> <span class="o">=</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">arctan</span><span class="p">(</span><span class="n">n</span> <span class="o">/</span> <span class="n">m</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">d</span> <span class="o">/</span> <span class="n">mu</span> <span class="o">/</span> <span class="n">lam</span><span class="p">))</span> \
                                        <span class="o">/</span> <span class="n">np</span><span class="o">.</span><span class="n">pi</span> <span class="o">*</span> <span class="mi">180</span>
                            <span class="n">sigmas</span><span class="p">[</span><span class="n">sigma</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="n">angle</span><span class="p">]</span>
                        <span class="k">else</span><span class="p">:</span>
                            <span class="k">if</span> <span class="n">m</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                                <span class="n">angle</span> <span class="o">=</span> <span class="mf">180.0</span>
                            <span class="k">else</span><span class="p">:</span>
                                <span class="n">angle</span> <span class="o">=</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">arctan</span><span class="p">(</span><span class="n">n</span> <span class="o">/</span> <span class="n">m</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">d</span> <span class="o">/</span> <span class="n">mu</span> <span class="o">/</span> <span class="n">lam</span><span class="p">))</span> \
                                        <span class="o">/</span> <span class="n">np</span><span class="o">.</span><span class="n">pi</span> <span class="o">*</span> <span class="mi">180</span>
                            <span class="k">if</span> <span class="n">angle</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">sigmas</span><span class="p">[</span><span class="n">sigma</span><span class="p">]:</span>
                                <span class="n">sigmas</span><span class="p">[</span><span class="n">sigma</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">angle</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">m_max</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="k">break</span>

        <span class="k">return</span> <span class="n">sigmas</span></div>

<div class="viewcode-block" id="GrainBoundaryGenerator.enum_possible_plane_cubic"><a class="viewcode-back" href="../../../../pymatgen.analysis.gb.grain.html#pymatgen.analysis.gb.grain.GrainBoundaryGenerator.enum_possible_plane_cubic">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">enum_possible_plane_cubic</span><span class="p">(</span><span class="n">plane_cutoff</span><span class="p">,</span> <span class="n">r_axis</span><span class="p">,</span> <span class="n">r_angle</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Find all possible plane combinations for GBs given a rotaion axis and angle for</span>
<span class="sd">        cubic system, and classify them to different categories, including &#39;Twist&#39;,</span>
<span class="sd">        &#39;Symmetric tilt&#39;, &#39;Normal tilt&#39;, &#39;Mixed&#39; GBs.</span>

<span class="sd">        Args:</span>
<span class="sd">            plane_cutoff (integer): the cutoff of plane miller index.</span>
<span class="sd">            r_axis (list of three integers, e.g. u, v, w):</span>
<span class="sd">                    the rotation axis of the grain boundary, with the format of [u,v,w].</span>
<span class="sd">            r_angle (float): rotation angle of the GBs.</span>
<span class="sd">        Returns:</span>
<span class="sd">            all_combinations (dict):</span>
<span class="sd">                    dictionary with keys as GB type, e.g. &#39;Twist&#39;,&#39;Symmetric tilt&#39;,etc.</span>
<span class="sd">                    and values as the combination of the two plane miller index</span>
<span class="sd">                     (GB plane and joining plane).</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">all_combinations</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="n">all_combinations</span><span class="p">[</span><span class="s1">&#39;Symmetric tilt&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">all_combinations</span><span class="p">[</span><span class="s1">&#39;Twist&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">all_combinations</span><span class="p">[</span><span class="s1">&#39;Normal tilt&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">all_combinations</span><span class="p">[</span><span class="s1">&#39;Mixed&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">sym_plane</span> <span class="o">=</span> <span class="n">symm_group_cubic</span><span class="p">([[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">]])</span>
        <span class="n">j</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">plane_cutoff</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
        <span class="n">combination</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">itertools</span><span class="o">.</span><span class="n">product</span><span class="p">(</span><span class="n">j</span><span class="p">,</span> <span class="n">repeat</span><span class="o">=</span><span class="mi">3</span><span class="p">):</span>
            <span class="k">if</span> <span class="nb">sum</span><span class="p">(</span><span class="nb">abs</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">i</span><span class="p">)))</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">combination</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="n">i</span><span class="p">))</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">nonzero</span><span class="p">(</span><span class="n">i</span><span class="p">)[</span><span class="mi">0</span><span class="p">])</span> <span class="o">==</span> <span class="mi">3</span><span class="p">:</span>
                <span class="k">for</span> <span class="n">i1</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">3</span><span class="p">):</span>
                    <span class="n">new_i</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">i</span><span class="p">)</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
                    <span class="n">new_i</span><span class="p">[</span><span class="n">i1</span><span class="p">]</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span> <span class="o">*</span> <span class="n">new_i</span><span class="p">[</span><span class="n">i1</span><span class="p">]</span>
                    <span class="n">combination</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">new_i</span><span class="p">)</span>
            <span class="k">elif</span> <span class="nb">len</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">nonzero</span><span class="p">(</span><span class="n">i</span><span class="p">)[</span><span class="mi">0</span><span class="p">])</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
                <span class="n">new_i</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">i</span><span class="p">)</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
                <span class="n">new_i</span><span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">nonzero</span><span class="p">(</span><span class="n">i</span><span class="p">)[</span><span class="mi">0</span><span class="p">][</span><span class="mi">0</span><span class="p">]]</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span> <span class="o">*</span> <span class="n">new_i</span><span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">nonzero</span><span class="p">(</span><span class="n">i</span><span class="p">)[</span><span class="mi">0</span><span class="p">][</span><span class="mi">0</span><span class="p">]]</span>
                <span class="n">combination</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">new_i</span><span class="p">)</span>
        <span class="n">miller</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">combination</span><span class="p">)</span>
        <span class="n">miller</span> <span class="o">=</span> <span class="n">miller</span><span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">argsort</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">norm</span><span class="p">(</span><span class="n">miller</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">))]</span>
        <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">val</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">miller</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">reduce</span><span class="p">(</span><span class="n">gcd</span><span class="p">,</span> <span class="n">val</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
                <span class="n">matrix</span> <span class="o">=</span> <span class="n">GrainBoundaryGenerator</span><span class="o">.</span><span class="n">get_trans_mat</span><span class="p">(</span><span class="n">r_axis</span><span class="p">,</span> <span class="n">r_angle</span><span class="p">,</span> <span class="n">surface</span><span class="o">=</span><span class="n">val</span><span class="p">,</span> <span class="n">quick_gen</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
                <span class="n">vec</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">cross</span><span class="p">(</span><span class="n">matrix</span><span class="p">[</span><span class="mi">1</span><span class="p">][</span><span class="mi">0</span><span class="p">],</span> <span class="n">matrix</span><span class="p">[</span><span class="mi">1</span><span class="p">][</span><span class="mi">1</span><span class="p">])</span>
                <span class="n">miller2</span> <span class="o">=</span> <span class="n">GrainBoundaryGenerator</span><span class="o">.</span><span class="n">vec_to_surface</span><span class="p">(</span><span class="n">vec</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">np</span><span class="o">.</span><span class="n">all</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">abs</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">miller2</span><span class="p">))</span> <span class="o">&lt;=</span> <span class="n">plane_cutoff</span><span class="p">):</span>
                    <span class="n">cos_1</span> <span class="o">=</span> <span class="nb">abs</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">val</span><span class="p">,</span> <span class="n">r_axis</span><span class="p">)</span> <span class="o">/</span> <span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">norm</span><span class="p">(</span><span class="n">val</span><span class="p">)</span> <span class="o">/</span> <span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">norm</span><span class="p">(</span><span class="n">r_axis</span><span class="p">))</span>
                    <span class="k">if</span> <span class="mi">1</span> <span class="o">-</span> <span class="n">cos_1</span> <span class="o">&lt;</span> <span class="mf">1.e-5</span><span class="p">:</span>
                        <span class="n">all_combinations</span><span class="p">[</span><span class="s1">&#39;Twist&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">([</span><span class="nb">list</span><span class="p">(</span><span class="n">val</span><span class="p">),</span> <span class="n">miller2</span><span class="p">])</span>
                    <span class="k">elif</span> <span class="n">cos_1</span> <span class="o">&lt;</span> <span class="mf">1.e-8</span><span class="p">:</span>
                        <span class="n">sym_tilt</span> <span class="o">=</span> <span class="kc">False</span>
                        <span class="k">if</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">abs</span><span class="p">(</span><span class="n">val</span><span class="p">))</span> <span class="o">==</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">abs</span><span class="p">(</span><span class="n">miller2</span><span class="p">)):</span>
                            <span class="n">ave</span> <span class="o">=</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">val</span><span class="p">)</span> <span class="o">+</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">miller2</span><span class="p">))</span> <span class="o">/</span> <span class="mi">2</span>
                            <span class="n">ave1</span> <span class="o">=</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">val</span><span class="p">)</span> <span class="o">-</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">miller2</span><span class="p">))</span> <span class="o">/</span> <span class="mi">2</span>
                            <span class="k">for</span> <span class="n">plane</span> <span class="ow">in</span> <span class="n">sym_plane</span><span class="p">:</span>
                                <span class="n">cos_2</span> <span class="o">=</span> <span class="nb">abs</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">ave</span><span class="p">,</span> <span class="n">plane</span><span class="p">)</span> <span class="o">/</span> <span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">norm</span><span class="p">(</span><span class="n">ave</span><span class="p">)</span> <span class="o">/</span> <span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">norm</span><span class="p">(</span><span class="n">plane</span><span class="p">))</span>
                                <span class="n">cos_3</span> <span class="o">=</span> <span class="nb">abs</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">ave1</span><span class="p">,</span> <span class="n">plane</span><span class="p">)</span> <span class="o">/</span> <span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">norm</span><span class="p">(</span><span class="n">ave1</span><span class="p">)</span> <span class="o">/</span> <span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">norm</span><span class="p">(</span><span class="n">plane</span><span class="p">))</span>
                                <span class="k">if</span> <span class="mi">1</span> <span class="o">-</span> <span class="n">cos_2</span> <span class="o">&lt;</span> <span class="mf">1.e-5</span> <span class="ow">or</span> <span class="mi">1</span> <span class="o">-</span> <span class="n">cos_3</span> <span class="o">&lt;</span> <span class="mf">1.e-5</span><span class="p">:</span>
                                    <span class="n">all_combinations</span><span class="p">[</span><span class="s1">&#39;Symmetric tilt&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">([</span><span class="nb">list</span><span class="p">(</span><span class="n">val</span><span class="p">),</span> <span class="n">miller2</span><span class="p">])</span>
                                    <span class="n">sym_tilt</span> <span class="o">=</span> <span class="kc">True</span>
                                    <span class="k">break</span>
                        <span class="k">if</span> <span class="ow">not</span> <span class="n">sym_tilt</span><span class="p">:</span>
                            <span class="n">all_combinations</span><span class="p">[</span><span class="s1">&#39;Normal tilt&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">([</span><span class="nb">list</span><span class="p">(</span><span class="n">val</span><span class="p">),</span> <span class="n">miller2</span><span class="p">])</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="n">all_combinations</span><span class="p">[</span><span class="s1">&#39;Mixed&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">([</span><span class="nb">list</span><span class="p">(</span><span class="n">val</span><span class="p">),</span> <span class="n">miller2</span><span class="p">])</span>
        <span class="k">return</span> <span class="n">all_combinations</span></div>

<div class="viewcode-block" id="GrainBoundaryGenerator.get_rotation_angle_from_sigma"><a class="viewcode-back" href="../../../../pymatgen.analysis.gb.grain.html#pymatgen.analysis.gb.grain.GrainBoundaryGenerator.get_rotation_angle_from_sigma">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">get_rotation_angle_from_sigma</span><span class="p">(</span><span class="n">sigma</span><span class="p">,</span> <span class="n">r_axis</span><span class="p">,</span> <span class="n">lat_type</span><span class="o">=</span><span class="s1">&#39;C&#39;</span><span class="p">,</span> <span class="n">ratio</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Find all possible rotation angle for the given sigma value.</span>

<span class="sd">        Args:</span>
<span class="sd">            sigma (integer):</span>
<span class="sd">                    sigma value provided</span>
<span class="sd">            r_axis (list of three integers, e.g. u, v, w</span>
<span class="sd">                    or four integers, e.g. u, v, t, w for hex/rho system only):</span>
<span class="sd">                    the rotation axis of the grain boundary.</span>
<span class="sd">            lat_type ( one character):</span>
<span class="sd">                    &#39;c&#39; or &#39;C&#39;: cubic system</span>
<span class="sd">                     &#39;t&#39; or &#39;T&#39;: tetragonal system</span>
<span class="sd">                     &#39;o&#39; or &#39;O&#39;: orthorhombic system</span>
<span class="sd">                     &#39;h&#39; or &#39;H&#39;: hexagonal system</span>
<span class="sd">                     &#39;r&#39; or &#39;R&#39;: rhombohedral system</span>
<span class="sd">                     default to cubic system</span>
<span class="sd">            ratio (list of integers):</span>
<span class="sd">                    lattice axial ratio.</span>
<span class="sd">                    For cubic system, ratio is not needed.</span>
<span class="sd">                    For tetragonal system, ratio = [mu, mv], list of two integers,</span>
<span class="sd">                    that is, mu/mv = c2/a2. If it is irrational, set it to none.</span>
<span class="sd">                    For orthorhombic system, ratio = [mu, lam, mv], list of three integers,</span>
<span class="sd">                    that is, mu:lam:mv = c2:b2:a2. If irrational for one axis, set it to None.</span>
<span class="sd">                    e.g. mu:lam:mv = c2,None,a2, means b2 is irrational.</span>
<span class="sd">                    For rhombohedral system, ratio = [mu, mv], list of two integers,</span>
<span class="sd">                    that is, mu/mv is the ratio of (1+2*cos(alpha)/cos(alpha).</span>
<span class="sd">                    If irrational, set it to None.</span>
<span class="sd">                    For hexagonal system, ratio = [mu, mv], list of two integers,</span>
<span class="sd">                    that is, mu/mv = c2/a2. If it is irrational, set it to none.</span>

<span class="sd">        Returns:</span>
<span class="sd">            rotation_angles corresponding to the provided sigma value.</span>
<span class="sd">            If the sigma value is not correct, return the rotation angle corresponding</span>
<span class="sd">            to the correct possible sigma value right smaller than the wrong sigma value provided.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">lat_type</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="o">==</span> <span class="s1">&#39;c&#39;</span><span class="p">:</span>
            <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;Make sure this is for cubic system&#39;</span><span class="p">)</span>
            <span class="n">sigma_dict</span> <span class="o">=</span> <span class="n">GrainBoundaryGenerator</span><span class="o">.</span><span class="n">enum_sigma_cubic</span><span class="p">(</span><span class="n">cutoff</span><span class="o">=</span><span class="n">sigma</span><span class="p">,</span> <span class="n">r_axis</span><span class="o">=</span><span class="n">r_axis</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">lat_type</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="o">==</span> <span class="s1">&#39;t&#39;</span><span class="p">:</span>
            <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;Make sure this is for tetragonal system&#39;</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">ratio</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;Make sure this is for irrational c2/a2 ratio&#39;</span><span class="p">)</span>
            <span class="k">elif</span> <span class="nb">len</span><span class="p">(</span><span class="n">ratio</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">2</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s1">&#39;Tetragonal system needs correct c2/a2 ratio&#39;</span><span class="p">)</span>
            <span class="n">sigma_dict</span> <span class="o">=</span> <span class="n">GrainBoundaryGenerator</span><span class="o">.</span><span class="n">enum_sigma_tet</span><span class="p">(</span><span class="n">cutoff</span><span class="o">=</span><span class="n">sigma</span><span class="p">,</span> <span class="n">r_axis</span><span class="o">=</span><span class="n">r_axis</span><span class="p">,</span> <span class="n">c2_a2_ratio</span><span class="o">=</span><span class="n">ratio</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">lat_type</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="o">==</span> <span class="s1">&#39;o&#39;</span><span class="p">:</span>
            <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;Make sure this is for orthorhombic system&#39;</span><span class="p">)</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">ratio</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">3</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s1">&#39;Orthorhombic system needs correct c2:b2:a2 ratio&#39;</span><span class="p">)</span>
            <span class="n">sigma_dict</span> <span class="o">=</span> <span class="n">GrainBoundaryGenerator</span><span class="o">.</span><span class="n">enum_sigma_ort</span><span class="p">(</span><span class="n">cutoff</span><span class="o">=</span><span class="n">sigma</span><span class="p">,</span> <span class="n">r_axis</span><span class="o">=</span><span class="n">r_axis</span><span class="p">,</span> <span class="n">c2_b2_a2_ratio</span><span class="o">=</span><span class="n">ratio</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">lat_type</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="o">==</span> <span class="s1">&#39;h&#39;</span><span class="p">:</span>
            <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;Make sure this is for hexagonal system&#39;</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">ratio</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;Make sure this is for irrational c2/a2 ratio&#39;</span><span class="p">)</span>
            <span class="k">elif</span> <span class="nb">len</span><span class="p">(</span><span class="n">ratio</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">2</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s1">&#39;Hexagonal system needs correct c2/a2 ratio&#39;</span><span class="p">)</span>
            <span class="n">sigma_dict</span> <span class="o">=</span> <span class="n">GrainBoundaryGenerator</span><span class="o">.</span><span class="n">enum_sigma_hex</span><span class="p">(</span><span class="n">cutoff</span><span class="o">=</span><span class="n">sigma</span><span class="p">,</span> <span class="n">r_axis</span><span class="o">=</span><span class="n">r_axis</span><span class="p">,</span> <span class="n">c2_a2_ratio</span><span class="o">=</span><span class="n">ratio</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">lat_type</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="o">==</span> <span class="s1">&#39;r&#39;</span><span class="p">:</span>
            <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;Make sure this is for rhombohedral system&#39;</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">ratio</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;Make sure this is for irrational (1+2*cos(alpha)/cos(alpha) ratio&#39;</span><span class="p">)</span>
            <span class="k">elif</span> <span class="nb">len</span><span class="p">(</span><span class="n">ratio</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">2</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s1">&#39;Rhombohedral system needs correct &#39;</span>
                                   <span class="s1">&#39;(1+2*cos(alpha)/cos(alpha) ratio&#39;</span><span class="p">)</span>
            <span class="n">sigma_dict</span> <span class="o">=</span> <span class="n">GrainBoundaryGenerator</span><span class="o">.</span><span class="n">enum_sigma_rho</span><span class="p">(</span><span class="n">cutoff</span><span class="o">=</span><span class="n">sigma</span><span class="p">,</span> <span class="n">r_axis</span><span class="o">=</span><span class="n">r_axis</span><span class="p">,</span> <span class="n">ratio_alpha</span><span class="o">=</span><span class="n">ratio</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s1">&#39;Lattice type not implemented&#39;</span><span class="p">)</span>

        <span class="n">sigmas</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">sigma_dict</span><span class="o">.</span><span class="n">keys</span><span class="p">())</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">sigmas</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s1">&#39;This is a wriong sigma value, and no sigma exists smaller than this value.&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">sigma</span> <span class="ow">in</span> <span class="n">sigmas</span><span class="p">:</span>
            <span class="n">rotation_angles</span> <span class="o">=</span> <span class="n">sigma_dict</span><span class="p">[</span><span class="n">sigma</span><span class="p">]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">sigmas</span><span class="o">.</span><span class="n">sort</span><span class="p">()</span>
            <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s2">&quot;This is not the possible sigma value according to the rotation axis!&quot;</span>
                          <span class="s2">&quot;The nearest neighbor sigma and its corresponding angle are returned&quot;</span><span class="p">)</span>
            <span class="n">rotation_angles</span> <span class="o">=</span> <span class="n">sigma_dict</span><span class="p">[</span><span class="n">sigmas</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]]</span>
        <span class="n">rotation_angles</span><span class="o">.</span><span class="n">sort</span><span class="p">()</span>
        <span class="k">return</span> <span class="n">rotation_angles</span></div>

<div class="viewcode-block" id="GrainBoundaryGenerator.slab_from_csl"><a class="viewcode-back" href="../../../../pymatgen.analysis.gb.grain.html#pymatgen.analysis.gb.grain.GrainBoundaryGenerator.slab_from_csl">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">slab_from_csl</span><span class="p">(</span><span class="n">csl</span><span class="p">,</span> <span class="n">surface</span><span class="p">,</span> <span class="n">normal</span><span class="p">,</span> <span class="n">trans_cry</span><span class="p">,</span> <span class="n">max_search</span><span class="o">=</span><span class="mi">20</span><span class="p">,</span> <span class="n">quick_gen</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        By linear operation of csl lattice vectors to get the best corresponding</span>
<span class="sd">        slab lattice. That is the area of a,b vectors (within the surface plane)</span>
<span class="sd">        is the smallest, the c vector first, has shortest length perpendicular</span>
<span class="sd">        to surface [h,k,l], second, has shortest length itself.</span>

<span class="sd">        Args:</span>
<span class="sd">            csl (3 by 3 integer array):</span>
<span class="sd">                    input csl lattice.</span>
<span class="sd">            surface (list of three integers, e.g. h, k, l):</span>
<span class="sd">                    the miller index of the surface, with the format of [h,k,l]</span>
<span class="sd">            normal (logic):</span>
<span class="sd">                    determine if the c vector needs to perpendicular to surface</span>
<span class="sd">            trans_cry (3 by 3 array):</span>
<span class="sd">                    transform matrix from crystal system to orthogonal system</span>
<span class="sd">            max_search (int): max search for the GB lattice vectors that give the smallest GB</span>
<span class="sd">                lattice. If normal is true, also max search the GB c vector that perpendicular</span>
<span class="sd">                to the plane.</span>
<span class="sd">            quick_gen (bool): whether to quickly generate a supercell, no need to find the smallest</span>
<span class="sd">                cell if set to true.</span>

<span class="sd">        Returns:</span>
<span class="sd">            t_matrix: a slab lattice ( 3 by 3 integer array):</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="c1"># set the transform matrix in real space</span>
        <span class="n">trans</span> <span class="o">=</span> <span class="n">trans_cry</span>
        <span class="c1"># transform matrix in reciprocal space</span>
        <span class="n">ctrans</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">inv</span><span class="p">(</span><span class="n">trans</span><span class="o">.</span><span class="n">T</span><span class="p">)</span>

        <span class="n">t_matrix</span> <span class="o">=</span> <span class="n">csl</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
        <span class="c1"># vectors constructed from csl that perpendicular to surface</span>
        <span class="n">ab_vector</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="c1"># obtain the miller index of surface in terms of csl.</span>
        <span class="n">miller</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">matmul</span><span class="p">(</span><span class="n">surface</span><span class="p">,</span> <span class="n">csl</span><span class="o">.</span><span class="n">T</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">reduce</span><span class="p">(</span><span class="n">gcd</span><span class="p">,</span> <span class="n">miller</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">1</span><span class="p">:</span>
            <span class="n">miller</span> <span class="o">=</span> <span class="p">[</span><span class="nb">int</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">x</span> <span class="o">/</span> <span class="n">reduce</span><span class="p">(</span><span class="n">gcd</span><span class="p">,</span> <span class="n">miller</span><span class="p">)))</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">miller</span><span class="p">]</span>
        <span class="n">miller_nonzero</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="c1"># quickly generate a supercell, normal is not work in this way</span>
        <span class="k">if</span> <span class="n">quick_gen</span><span class="p">:</span>
            <span class="n">scale_factor</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="n">eye</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">eye</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">int</span><span class="p">)</span>
            <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">miller</span><span class="p">):</span>
                <span class="k">if</span> <span class="n">j</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                    <span class="n">scale_factor</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">eye</span><span class="p">[</span><span class="n">i</span><span class="p">])</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">miller_nonzero</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">scale_factor</span><span class="p">)</span> <span class="o">&lt;</span> <span class="mi">2</span><span class="p">:</span>
                <span class="n">index_len</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">miller_nonzero</span><span class="p">)</span>
                <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">index_len</span><span class="p">):</span>
                    <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">index_len</span><span class="p">):</span>
                        <span class="n">lcm_miller</span> <span class="o">=</span> <span class="n">lcm</span><span class="p">(</span><span class="n">miller</span><span class="p">[</span><span class="n">miller_nonzero</span><span class="p">[</span><span class="n">i</span><span class="p">]],</span> <span class="n">miller</span><span class="p">[</span><span class="n">miller_nonzero</span><span class="p">[</span><span class="n">j</span><span class="p">]])</span>
                        <span class="n">l</span> <span class="o">=</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">]</span>
                        <span class="n">l</span><span class="p">[</span><span class="n">miller_nonzero</span><span class="p">[</span><span class="n">i</span><span class="p">]]</span> <span class="o">=</span> <span class="o">-</span><span class="nb">int</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">lcm_miller</span> <span class="o">/</span> <span class="n">miller</span><span class="p">[</span><span class="n">miller_nonzero</span><span class="p">[</span><span class="n">i</span><span class="p">]]))</span>
                        <span class="n">l</span><span class="p">[</span><span class="n">miller_nonzero</span><span class="p">[</span><span class="n">j</span><span class="p">]]</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">lcm_miller</span> <span class="o">/</span> <span class="n">miller</span><span class="p">[</span><span class="n">miller_nonzero</span><span class="p">[</span><span class="n">j</span><span class="p">]]))</span>
                        <span class="n">scale_factor</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">l</span><span class="p">)</span>
                        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">scale_factor</span><span class="p">)</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
                            <span class="k">break</span>
            <span class="n">t_matrix</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">scale_factor</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">csl</span><span class="p">))</span>
            <span class="n">t_matrix</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">scale_factor</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">csl</span><span class="p">))</span>
            <span class="n">t_matrix</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="n">csl</span><span class="p">[</span><span class="n">miller_nonzero</span><span class="p">[</span><span class="mi">0</span><span class="p">]]</span>
            <span class="k">if</span> <span class="nb">abs</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">det</span><span class="p">(</span><span class="n">t_matrix</span><span class="p">))</span> <span class="o">&gt;</span> <span class="mi">1000</span><span class="p">:</span>
                <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s1">&#39;Too large matrix. Suggest to use quick_gen=False&#39;</span><span class="p">)</span>
            <span class="k">return</span> <span class="n">t_matrix</span>

        <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">miller</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">j</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">ab_vector</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">csl</span><span class="p">[</span><span class="n">i</span><span class="p">])</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">c_index</span> <span class="o">=</span> <span class="n">i</span>
                <span class="n">miller_nonzero</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">j</span><span class="p">)</span>

        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">miller_nonzero</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
            <span class="n">t_matrix</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="n">csl</span><span class="p">[</span><span class="n">c_index</span><span class="p">]</span>
            <span class="n">index_len</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">miller_nonzero</span><span class="p">)</span>
            <span class="n">lcm_miller</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">index_len</span><span class="p">):</span>
                <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">index_len</span><span class="p">):</span>
                    <span class="n">com_gcd</span> <span class="o">=</span> <span class="n">gcd</span><span class="p">(</span><span class="n">miller_nonzero</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="n">miller_nonzero</span><span class="p">[</span><span class="n">j</span><span class="p">])</span>
                    <span class="n">mil1</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">miller_nonzero</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">/</span> <span class="n">com_gcd</span><span class="p">))</span>
                    <span class="n">mil2</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">miller_nonzero</span><span class="p">[</span><span class="n">j</span><span class="p">]</span> <span class="o">/</span> <span class="n">com_gcd</span><span class="p">))</span>
                    <span class="n">lcm_miller</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">max</span><span class="p">(</span><span class="nb">abs</span><span class="p">(</span><span class="n">mil1</span><span class="p">),</span> <span class="nb">abs</span><span class="p">(</span><span class="n">mil2</span><span class="p">)))</span>
            <span class="n">lcm_sorted</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">lcm_miller</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">index_len</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
                <span class="n">max_j</span> <span class="o">=</span> <span class="n">lcm_sorted</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">max_j</span> <span class="o">=</span> <span class="n">lcm_sorted</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">normal</span><span class="p">:</span>
                <span class="n">t_matrix</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="n">ab_vector</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
                <span class="n">t_matrix</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">ab_vector</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
                <span class="n">t_matrix</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="n">csl</span><span class="p">[</span><span class="n">c_index</span><span class="p">]</span>
                <span class="k">return</span> <span class="n">t_matrix</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">max_j</span> <span class="o">=</span> <span class="nb">abs</span><span class="p">(</span><span class="n">miller_nonzero</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
        <span class="k">if</span> <span class="n">max_j</span> <span class="o">&gt;</span> <span class="n">max_search</span><span class="p">:</span>
            <span class="n">max_j</span> <span class="o">=</span> <span class="n">max_search</span>
        <span class="c1"># area of a, b vectors</span>
        <span class="n">area</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="c1"># length of c vector</span>
        <span class="n">c_norm</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">norm</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">matmul</span><span class="p">(</span><span class="n">t_matrix</span><span class="p">[</span><span class="mi">2</span><span class="p">],</span> <span class="n">trans</span><span class="p">))</span>
        <span class="c1"># c vector length along the direction perpendicular to surface</span>
        <span class="n">c_length</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">abs</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">t_matrix</span><span class="p">[</span><span class="mi">2</span><span class="p">],</span> <span class="n">surface</span><span class="p">))</span>
        <span class="c1"># check if the init c vector perpendicular to the surface</span>
        <span class="k">if</span> <span class="n">normal</span><span class="p">:</span>
            <span class="n">c_cross</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">cross</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">matmul</span><span class="p">(</span><span class="n">t_matrix</span><span class="p">[</span><span class="mi">2</span><span class="p">],</span> <span class="n">trans</span><span class="p">),</span> <span class="n">np</span><span class="o">.</span><span class="n">matmul</span><span class="p">(</span><span class="n">surface</span><span class="p">,</span> <span class="n">ctrans</span><span class="p">))</span>
            <span class="k">if</span> <span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">norm</span><span class="p">(</span><span class="n">c_cross</span><span class="p">)</span> <span class="o">&lt;</span> <span class="mf">1.e-8</span><span class="p">:</span>
                <span class="n">normal_init</span> <span class="o">=</span> <span class="kc">True</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">normal_init</span> <span class="o">=</span> <span class="kc">False</span>

        <span class="n">j</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">max_j</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
        <span class="n">combination</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">itertools</span><span class="o">.</span><span class="n">product</span><span class="p">(</span><span class="n">j</span><span class="p">,</span> <span class="n">repeat</span><span class="o">=</span><span class="mi">3</span><span class="p">):</span>
            <span class="k">if</span> <span class="nb">sum</span><span class="p">(</span><span class="nb">abs</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">i</span><span class="p">)))</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">combination</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="n">i</span><span class="p">))</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">nonzero</span><span class="p">(</span><span class="n">i</span><span class="p">)[</span><span class="mi">0</span><span class="p">])</span> <span class="o">==</span> <span class="mi">3</span><span class="p">:</span>
                <span class="k">for</span> <span class="n">i1</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">3</span><span class="p">):</span>
                    <span class="n">new_i</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">i</span><span class="p">)</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
                    <span class="n">new_i</span><span class="p">[</span><span class="n">i1</span><span class="p">]</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span> <span class="o">*</span> <span class="n">new_i</span><span class="p">[</span><span class="n">i1</span><span class="p">]</span>
                    <span class="n">combination</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">new_i</span><span class="p">)</span>
            <span class="k">elif</span> <span class="nb">len</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">nonzero</span><span class="p">(</span><span class="n">i</span><span class="p">)[</span><span class="mi">0</span><span class="p">])</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
                <span class="n">new_i</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">i</span><span class="p">)</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
                <span class="n">new_i</span><span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">nonzero</span><span class="p">(</span><span class="n">i</span><span class="p">)[</span><span class="mi">0</span><span class="p">][</span><span class="mi">0</span><span class="p">]]</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span> <span class="o">*</span> <span class="n">new_i</span><span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">nonzero</span><span class="p">(</span><span class="n">i</span><span class="p">)[</span><span class="mi">0</span><span class="p">][</span><span class="mi">0</span><span class="p">]]</span>
                <span class="n">combination</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">new_i</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">combination</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">reduce</span><span class="p">(</span><span class="n">gcd</span><span class="p">,</span> <span class="n">i</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
                <span class="n">temp</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">i</span><span class="p">),</span> <span class="n">csl</span><span class="p">)</span>
                <span class="k">if</span> <span class="nb">abs</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">temp</span><span class="p">,</span> <span class="n">surface</span><span class="p">)</span> <span class="o">-</span> <span class="mi">0</span><span class="p">)</span> <span class="o">&lt;</span> <span class="mf">1.e-8</span><span class="p">:</span>
                    <span class="n">ab_vector</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">temp</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="c1"># c vector length along the direction perpendicular to surface</span>
                    <span class="n">c_len_temp</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">abs</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">temp</span><span class="p">,</span> <span class="n">surface</span><span class="p">))</span>
                    <span class="c1"># c vector length itself</span>
                    <span class="n">c_norm_temp</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">norm</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">matmul</span><span class="p">(</span><span class="n">temp</span><span class="p">,</span> <span class="n">trans</span><span class="p">))</span>
                    <span class="k">if</span> <span class="n">normal</span><span class="p">:</span>
                        <span class="n">c_cross</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">cross</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">matmul</span><span class="p">(</span><span class="n">temp</span><span class="p">,</span> <span class="n">trans</span><span class="p">),</span> <span class="n">np</span><span class="o">.</span><span class="n">matmul</span><span class="p">(</span><span class="n">surface</span><span class="p">,</span> <span class="n">ctrans</span><span class="p">))</span>
                        <span class="k">if</span> <span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">norm</span><span class="p">(</span><span class="n">c_cross</span><span class="p">)</span> <span class="o">&lt;</span> <span class="mf">1.e-8</span><span class="p">:</span>
                            <span class="k">if</span> <span class="n">normal_init</span><span class="p">:</span>
                                <span class="k">if</span> <span class="n">c_norm_temp</span> <span class="o">&lt;</span> <span class="n">c_norm</span><span class="p">:</span>
                                    <span class="n">t_matrix</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="n">temp</span>
                                    <span class="n">c_norm</span> <span class="o">=</span> <span class="n">c_norm_temp</span>
                            <span class="k">else</span><span class="p">:</span>
                                <span class="n">c_norm</span> <span class="o">=</span> <span class="n">c_norm_temp</span>
                                <span class="n">normal_init</span> <span class="o">=</span> <span class="kc">True</span>
                                <span class="n">t_matrix</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="n">temp</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="k">if</span> <span class="n">c_len_temp</span> <span class="o">&lt;</span> <span class="n">c_length</span> <span class="ow">or</span> \
                                <span class="p">(</span><span class="nb">abs</span><span class="p">(</span><span class="n">c_len_temp</span> <span class="o">-</span> <span class="n">c_length</span><span class="p">)</span> <span class="o">&lt;</span> <span class="mf">1.e-8</span> <span class="ow">and</span> <span class="n">c_norm_temp</span> <span class="o">&lt;</span> <span class="n">c_norm</span><span class="p">):</span>
                            <span class="n">t_matrix</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="n">temp</span>
                            <span class="n">c_norm</span> <span class="o">=</span> <span class="n">c_norm_temp</span>
                            <span class="n">c_length</span> <span class="o">=</span> <span class="n">c_len_temp</span>

        <span class="k">if</span> <span class="n">normal</span> <span class="ow">and</span> <span class="p">(</span><span class="ow">not</span> <span class="n">normal_init</span><span class="p">):</span>
            <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;Did not find the perpendicular c vector, increase max_j&#39;</span><span class="p">)</span>
            <span class="k">while</span> <span class="p">(</span><span class="ow">not</span> <span class="n">normal_init</span><span class="p">):</span>
                <span class="k">if</span> <span class="n">max_j</span> <span class="o">==</span> <span class="n">max_search</span><span class="p">:</span>
                    <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s1">&#39;Cannot find the perpendicular c vector, please increase max_search&#39;</span><span class="p">)</span>
                    <span class="k">break</span>
                <span class="n">max_j</span> <span class="o">=</span> <span class="mi">3</span> <span class="o">*</span> <span class="n">max_j</span>
                <span class="k">if</span> <span class="n">max_j</span> <span class="o">&gt;</span> <span class="n">max_search</span><span class="p">:</span>
                    <span class="n">max_j</span> <span class="o">=</span> <span class="n">max_search</span>
                <span class="n">j</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">max_j</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
                <span class="n">combination</span> <span class="o">=</span> <span class="p">[]</span>
                <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">itertools</span><span class="o">.</span><span class="n">product</span><span class="p">(</span><span class="n">j</span><span class="p">,</span> <span class="n">repeat</span><span class="o">=</span><span class="mi">3</span><span class="p">):</span>
                    <span class="k">if</span> <span class="nb">sum</span><span class="p">(</span><span class="nb">abs</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">i</span><span class="p">)))</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">:</span>
                        <span class="n">combination</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="n">i</span><span class="p">))</span>
                    <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">nonzero</span><span class="p">(</span><span class="n">i</span><span class="p">)[</span><span class="mi">0</span><span class="p">])</span> <span class="o">==</span> <span class="mi">3</span><span class="p">:</span>
                        <span class="k">for</span> <span class="n">i1</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">3</span><span class="p">):</span>
                            <span class="n">new_i</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">i</span><span class="p">)</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
                            <span class="n">new_i</span><span class="p">[</span><span class="n">i1</span><span class="p">]</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span> <span class="o">*</span> <span class="n">new_i</span><span class="p">[</span><span class="n">i1</span><span class="p">]</span>
                            <span class="n">combination</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">new_i</span><span class="p">)</span>
                    <span class="k">elif</span> <span class="nb">len</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">nonzero</span><span class="p">(</span><span class="n">i</span><span class="p">)[</span><span class="mi">0</span><span class="p">])</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
                        <span class="n">new_i</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">i</span><span class="p">)</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
                        <span class="n">new_i</span><span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">nonzero</span><span class="p">(</span><span class="n">i</span><span class="p">)[</span><span class="mi">0</span><span class="p">][</span><span class="mi">0</span><span class="p">]]</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span> <span class="o">*</span> <span class="n">new_i</span><span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">nonzero</span><span class="p">(</span><span class="n">i</span><span class="p">)[</span><span class="mi">0</span><span class="p">][</span><span class="mi">0</span><span class="p">]]</span>
                        <span class="n">combination</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">new_i</span><span class="p">)</span>
                <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">combination</span><span class="p">:</span>
                    <span class="k">if</span> <span class="n">reduce</span><span class="p">(</span><span class="n">gcd</span><span class="p">,</span> <span class="n">i</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
                        <span class="n">temp</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">i</span><span class="p">),</span> <span class="n">csl</span><span class="p">)</span>
                        <span class="k">if</span> <span class="nb">abs</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">temp</span><span class="p">,</span> <span class="n">surface</span><span class="p">)</span> <span class="o">-</span> <span class="mi">0</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mf">1.e-8</span><span class="p">:</span>
                            <span class="n">c_cross</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">cross</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">matmul</span><span class="p">(</span><span class="n">temp</span><span class="p">,</span> <span class="n">trans</span><span class="p">),</span> <span class="n">np</span><span class="o">.</span><span class="n">matmul</span><span class="p">(</span><span class="n">surface</span><span class="p">,</span> <span class="n">ctrans</span><span class="p">))</span>
                            <span class="k">if</span> <span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">norm</span><span class="p">(</span><span class="n">c_cross</span><span class="p">)</span> <span class="o">&lt;</span> <span class="mf">1.e-8</span><span class="p">:</span>
                                <span class="c1"># c vetor length itself</span>
                                <span class="n">c_norm_temp</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">norm</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">matmul</span><span class="p">(</span><span class="n">temp</span><span class="p">,</span> <span class="n">trans</span><span class="p">))</span>
                                <span class="k">if</span> <span class="n">normal_init</span><span class="p">:</span>
                                    <span class="k">if</span> <span class="n">c_norm_temp</span> <span class="o">&lt;</span> <span class="n">c_norm</span><span class="p">:</span>
                                        <span class="n">t_matrix</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="n">temp</span>
                                        <span class="n">c_norm</span> <span class="o">=</span> <span class="n">c_norm_temp</span>
                                <span class="k">else</span><span class="p">:</span>
                                    <span class="n">c_norm</span> <span class="o">=</span> <span class="n">c_norm_temp</span>
                                    <span class="n">normal_init</span> <span class="o">=</span> <span class="kc">True</span>
                                    <span class="n">t_matrix</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="n">temp</span>
                <span class="k">if</span> <span class="n">normal_init</span><span class="p">:</span>
                    <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;Found perpendicular c vector&#39;</span><span class="p">)</span>

        <span class="c1"># find the best a, b vectors with their formed area smallest and average norm of a,b smallest.</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">itertools</span><span class="o">.</span><span class="n">combinations</span><span class="p">(</span><span class="n">ab_vector</span><span class="p">,</span> <span class="mi">2</span><span class="p">):</span>
            <span class="n">area_temp</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">norm</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">cross</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">matmul</span><span class="p">(</span><span class="n">i</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">trans</span><span class="p">),</span>
                                                <span class="n">np</span><span class="o">.</span><span class="n">matmul</span><span class="p">(</span><span class="n">i</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">trans</span><span class="p">)))</span>
            <span class="k">if</span> <span class="nb">abs</span><span class="p">(</span><span class="n">area_temp</span> <span class="o">-</span> <span class="mi">0</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mf">1.e-8</span><span class="p">:</span>
                <span class="n">ab_norm_temp</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">norm</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">matmul</span><span class="p">(</span><span class="n">i</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">trans</span><span class="p">))</span> <span class="o">+</span> \
                               <span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">norm</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">matmul</span><span class="p">(</span><span class="n">i</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">trans</span><span class="p">))</span>
                <span class="k">if</span> <span class="n">area</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                    <span class="n">area</span> <span class="o">=</span> <span class="n">area_temp</span>
                    <span class="n">ab_norm</span> <span class="o">=</span> <span class="n">ab_norm_temp</span>
                    <span class="n">t_matrix</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="n">i</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
                    <span class="n">t_matrix</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">i</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
                <span class="k">elif</span> <span class="n">area_temp</span> <span class="o">&lt;</span> <span class="n">area</span><span class="p">:</span>
                    <span class="n">t_matrix</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="n">i</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
                    <span class="n">t_matrix</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">i</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
                    <span class="n">area</span> <span class="o">=</span> <span class="n">area_temp</span>
                    <span class="n">ab_norm</span> <span class="o">=</span> <span class="n">ab_norm_temp</span>
                <span class="k">elif</span> <span class="nb">abs</span><span class="p">(</span><span class="n">area</span> <span class="o">-</span> <span class="n">area_temp</span><span class="p">)</span> <span class="o">&lt;</span> <span class="mf">1.e-8</span> <span class="ow">and</span> <span class="n">ab_norm_temp</span> <span class="o">&lt;</span> <span class="n">ab_norm</span><span class="p">:</span>
                    <span class="n">t_matrix</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="n">i</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
                    <span class="n">t_matrix</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">i</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
                    <span class="n">area</span> <span class="o">=</span> <span class="n">area_temp</span>
                    <span class="n">ab_norm</span> <span class="o">=</span> <span class="n">ab_norm_temp</span>

        <span class="c1"># make sure we have a left-handed crystallographic system</span>
        <span class="k">if</span> <span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">det</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">matmul</span><span class="p">(</span><span class="n">t_matrix</span><span class="p">,</span> <span class="n">trans</span><span class="p">))</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">:</span>
            <span class="n">t_matrix</span> <span class="o">*=</span> <span class="o">-</span><span class="mi">1</span>

        <span class="k">if</span> <span class="n">normal</span> <span class="ow">and</span> <span class="nb">abs</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">det</span><span class="p">(</span><span class="n">t_matrix</span><span class="p">))</span> <span class="o">&gt;</span> <span class="mi">1000</span><span class="p">:</span>
            <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s1">&#39;Too large matrix. Suggest to use Normal=False&#39;</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">t_matrix</span></div>

<div class="viewcode-block" id="GrainBoundaryGenerator.reduce_mat"><a class="viewcode-back" href="../../../../pymatgen.analysis.gb.grain.html#pymatgen.analysis.gb.grain.GrainBoundaryGenerator.reduce_mat">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">reduce_mat</span><span class="p">(</span><span class="n">mat</span><span class="p">,</span> <span class="n">mag</span><span class="p">,</span> <span class="n">r_matrix</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Reduce integer array mat&#39;s determinant mag times by linear combination</span>
<span class="sd">        of its row vectors, so that the new array after rotation (r_matrix) is</span>
<span class="sd">        still an integer array</span>

<span class="sd">        Args:</span>
<span class="sd">            mat (3 by 3 array): input matrix</span>
<span class="sd">            mag (integer): reduce times for the determinant</span>
<span class="sd">            r_matrix (3 by 3 array): rotation matrix</span>
<span class="sd">        Return:</span>
<span class="sd">            the reduced integer array</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">max_j</span> <span class="o">=</span> <span class="nb">abs</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">det</span><span class="p">(</span><span class="n">mat</span><span class="p">)</span> <span class="o">/</span> <span class="n">mag</span><span class="p">)))</span>
        <span class="n">reduced</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="k">for</span> <span class="n">h</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">3</span><span class="p">):</span>
            <span class="n">k</span> <span class="o">=</span> <span class="n">h</span> <span class="o">+</span> <span class="mi">1</span> <span class="k">if</span> <span class="n">h</span> <span class="o">+</span> <span class="mi">1</span> <span class="o">&lt;</span> <span class="mi">3</span> <span class="k">else</span> <span class="nb">abs</span><span class="p">(</span><span class="mi">2</span> <span class="o">-</span> <span class="n">h</span><span class="p">)</span>
            <span class="n">l</span> <span class="o">=</span> <span class="n">h</span> <span class="o">+</span> <span class="mi">2</span> <span class="k">if</span> <span class="n">h</span> <span class="o">+</span> <span class="mi">2</span> <span class="o">&lt;</span> <span class="mi">3</span> <span class="k">else</span> <span class="nb">abs</span><span class="p">(</span><span class="mi">1</span> <span class="o">-</span> <span class="n">h</span><span class="p">)</span>
            <span class="n">j</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="o">-</span><span class="n">max_j</span><span class="p">,</span> <span class="n">max_j</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
            <span class="k">for</span> <span class="n">j1</span><span class="p">,</span> <span class="n">j2</span> <span class="ow">in</span> <span class="n">itertools</span><span class="o">.</span><span class="n">product</span><span class="p">(</span><span class="n">j</span><span class="p">,</span> <span class="n">repeat</span><span class="o">=</span><span class="mi">2</span><span class="p">):</span>
                <span class="n">temp</span> <span class="o">=</span> <span class="n">mat</span><span class="p">[</span><span class="n">h</span><span class="p">]</span> <span class="o">+</span> <span class="n">j1</span> <span class="o">*</span> <span class="n">mat</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="o">+</span> <span class="n">j2</span> <span class="o">*</span> <span class="n">mat</span><span class="p">[</span><span class="n">l</span><span class="p">]</span>
                <span class="k">if</span> <span class="nb">all</span><span class="p">([</span><span class="n">np</span><span class="o">.</span><span class="n">round</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="mi">5</span><span class="p">)</span><span class="o">.</span><span class="n">is_integer</span><span class="p">()</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="nb">list</span><span class="p">(</span><span class="n">temp</span> <span class="o">/</span> <span class="n">mag</span><span class="p">)]):</span>
                    <span class="n">mat_copy</span> <span class="o">=</span> <span class="n">mat</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
                    <span class="n">mat_copy</span><span class="p">[</span><span class="n">h</span><span class="p">]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="nb">int</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">ele</span> <span class="o">/</span> <span class="n">mag</span><span class="p">))</span> <span class="k">for</span> <span class="n">ele</span> <span class="ow">in</span> <span class="n">temp</span><span class="p">])</span>
                    <span class="n">new_mat</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">mat_copy</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">inv</span><span class="p">(</span><span class="n">r_matrix</span><span class="o">.</span><span class="n">T</span><span class="p">))</span>
                    <span class="k">if</span> <span class="nb">all</span><span class="p">([</span><span class="n">np</span><span class="o">.</span><span class="n">round</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="mi">5</span><span class="p">)</span><span class="o">.</span><span class="n">is_integer</span><span class="p">()</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="nb">list</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">ravel</span><span class="p">(</span><span class="n">new_mat</span><span class="p">))]):</span>
                        <span class="n">reduced</span> <span class="o">=</span> <span class="kc">True</span>
                        <span class="n">mat</span><span class="p">[</span><span class="n">h</span><span class="p">]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="nb">int</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">ele</span> <span class="o">/</span> <span class="n">mag</span><span class="p">))</span> <span class="k">for</span> <span class="n">ele</span> <span class="ow">in</span> <span class="n">temp</span><span class="p">])</span>
                        <span class="k">break</span>
            <span class="k">if</span> <span class="n">reduced</span><span class="p">:</span>
                <span class="k">break</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="n">reduced</span><span class="p">:</span>
            <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s2">&quot;Matrix reduction not performed, may lead to non-primitive gb cell.&quot;</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">mat</span></div>

<div class="viewcode-block" id="GrainBoundaryGenerator.vec_to_surface"><a class="viewcode-back" href="../../../../pymatgen.analysis.gb.grain.html#pymatgen.analysis.gb.grain.GrainBoundaryGenerator.vec_to_surface">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">vec_to_surface</span><span class="p">(</span><span class="n">vec</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Transform a float vector to a surface miller index with integers.</span>

<span class="sd">        Args:</span>
<span class="sd">            vec (1 by 3 array float vector): input float vector</span>
<span class="sd">        Return:</span>
<span class="sd">            the surface miller index of the input vector.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">miller</span> <span class="o">=</span> <span class="p">[</span><span class="kc">None</span><span class="p">]</span> <span class="o">*</span> <span class="mi">3</span>
        <span class="n">index</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">value</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">vec</span><span class="p">):</span>
            <span class="k">if</span> <span class="nb">abs</span><span class="p">(</span><span class="n">value</span><span class="p">)</span> <span class="o">&lt;</span> <span class="mf">1.e-8</span><span class="p">:</span>
                <span class="n">miller</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">index</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">index</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
            <span class="n">miller</span><span class="p">[</span><span class="n">index</span><span class="p">[</span><span class="mi">0</span><span class="p">]]</span> <span class="o">=</span> <span class="mi">1</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">min_index</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">argmin</span><span class="p">([</span><span class="n">i</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">vec</span> <span class="k">if</span> <span class="n">i</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">])</span>
            <span class="n">true_index</span> <span class="o">=</span> <span class="n">index</span><span class="p">[</span><span class="n">min_index</span><span class="p">]</span>
            <span class="n">index</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="n">min_index</span><span class="p">)</span>
            <span class="n">frac</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">value</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">index</span><span class="p">):</span>
                <span class="n">frac</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">Fraction</span><span class="p">(</span><span class="n">vec</span><span class="p">[</span><span class="n">value</span><span class="p">]</span> <span class="o">/</span> <span class="n">vec</span><span class="p">[</span><span class="n">true_index</span><span class="p">])</span><span class="o">.</span><span class="n">limit_denominator</span><span class="p">(</span><span class="mi">100</span><span class="p">))</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">index</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
                <span class="n">miller</span><span class="p">[</span><span class="n">true_index</span><span class="p">]</span> <span class="o">=</span> <span class="n">frac</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">denominator</span>
                <span class="n">miller</span><span class="p">[</span><span class="n">index</span><span class="p">[</span><span class="mi">0</span><span class="p">]]</span> <span class="o">=</span> <span class="n">frac</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">numerator</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">com_lcm</span> <span class="o">=</span> <span class="n">lcm</span><span class="p">(</span><span class="n">frac</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">denominator</span><span class="p">,</span> <span class="n">frac</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">denominator</span><span class="p">)</span>
                <span class="n">miller</span><span class="p">[</span><span class="n">true_index</span><span class="p">]</span> <span class="o">=</span> <span class="n">com_lcm</span>
                <span class="n">miller</span><span class="p">[</span><span class="n">index</span><span class="p">[</span><span class="mi">0</span><span class="p">]]</span> <span class="o">=</span> <span class="n">frac</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">numerator</span> <span class="o">*</span> <span class="nb">int</span><span class="p">(</span><span class="nb">round</span><span class="p">((</span><span class="n">com_lcm</span> <span class="o">/</span> <span class="n">frac</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">denominator</span><span class="p">)))</span>
                <span class="n">miller</span><span class="p">[</span><span class="n">index</span><span class="p">[</span><span class="mi">1</span><span class="p">]]</span> <span class="o">=</span> <span class="n">frac</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">numerator</span> <span class="o">*</span> <span class="nb">int</span><span class="p">(</span><span class="nb">round</span><span class="p">((</span><span class="n">com_lcm</span> <span class="o">/</span> <span class="n">frac</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">denominator</span><span class="p">)))</span>
        <span class="k">return</span> <span class="n">miller</span></div></div>


<div class="viewcode-block" id="factors"><a class="viewcode-back" href="../../../../pymatgen.analysis.gb.grain.html#pymatgen.analysis.gb.grain.factors">[docs]</a><span class="k">def</span> <span class="nf">factors</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Compute the factors of a integer.</span>
<span class="sd">    Args:</span>
<span class="sd">        n: the input integer</span>

<span class="sd">    Returns:</span>
<span class="sd">        a set of integers that are the factors of the input integer.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="nb">set</span><span class="p">(</span><span class="n">reduce</span><span class="p">(</span><span class="nb">list</span><span class="o">.</span><span class="fm">__add__</span><span class="p">,</span>
                      <span class="p">([</span><span class="n">i</span><span class="p">,</span> <span class="n">n</span> <span class="o">//</span> <span class="n">i</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="nb">int</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">n</span><span class="p">))</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="k">if</span> <span class="n">n</span> <span class="o">%</span> <span class="n">i</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)))</span></div>


<div class="viewcode-block" id="fix_pbc"><a class="viewcode-back" href="../../../../pymatgen.analysis.gb.grain.html#pymatgen.analysis.gb.grain.fix_pbc">[docs]</a><span class="k">def</span> <span class="nf">fix_pbc</span><span class="p">(</span><span class="n">structure</span><span class="p">,</span> <span class="n">matrix</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Set all frac_coords of the input structure within [0,1].</span>

<span class="sd">    Args:</span>
<span class="sd">        structure (pymatgen structure object):</span>
<span class="sd">            input structure</span>
<span class="sd">        matrix (lattice matrix, 3 by 3 array/matrix)</span>
<span class="sd">            new structure&#39;s lattice matrix, if none, use</span>
<span class="sd">            input structure&#39;s matrix</span>

<span class="sd">    Return:</span>
<span class="sd">        new structure with fixed frac_coords and lattice matrix</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="n">spec</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="n">coords</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">if</span> <span class="n">matrix</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">latte</span> <span class="o">=</span> <span class="n">Lattice</span><span class="p">(</span><span class="n">structure</span><span class="o">.</span><span class="n">lattice</span><span class="o">.</span><span class="n">matrix</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">latte</span> <span class="o">=</span> <span class="n">Lattice</span><span class="p">(</span><span class="n">matrix</span><span class="p">)</span>

    <span class="k">for</span> <span class="n">site</span> <span class="ow">in</span> <span class="n">structure</span><span class="p">:</span>
        <span class="n">spec</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">site</span><span class="o">.</span><span class="n">specie</span><span class="p">)</span>
        <span class="n">coord</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">site</span><span class="o">.</span><span class="n">frac_coords</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">3</span><span class="p">):</span>
            <span class="n">coord</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">-=</span> <span class="n">floor</span><span class="p">(</span><span class="n">coord</span><span class="p">[</span><span class="n">i</span><span class="p">])</span>
            <span class="k">if</span> <span class="n">np</span><span class="o">.</span><span class="n">allclose</span><span class="p">(</span><span class="n">coord</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="mi">1</span><span class="p">):</span>
                <span class="n">coord</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span>
            <span class="k">elif</span> <span class="n">np</span><span class="o">.</span><span class="n">allclose</span><span class="p">(</span><span class="n">coord</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="mi">0</span><span class="p">):</span>
                <span class="n">coord</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">coord</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="nb">round</span><span class="p">(</span><span class="n">coord</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="mi">7</span><span class="p">)</span>
        <span class="n">coords</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">coord</span><span class="p">)</span>

    <span class="k">return</span> <span class="n">Structure</span><span class="p">(</span><span class="n">latte</span><span class="p">,</span> <span class="n">spec</span><span class="p">,</span> <span class="n">coords</span><span class="p">,</span> <span class="n">site_properties</span><span class="o">=</span><span class="n">structure</span><span class="o">.</span><span class="n">site_properties</span><span class="p">)</span></div>


<div class="viewcode-block" id="symm_group_cubic"><a class="viewcode-back" href="../../../../pymatgen.analysis.gb.grain.html#pymatgen.analysis.gb.grain.symm_group_cubic">[docs]</a><span class="k">def</span> <span class="nf">symm_group_cubic</span><span class="p">(</span><span class="n">mat</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">     obtain cubic symmetric eqivalents of the list of vectors.</span>

<span class="sd">    Args:</span>
<span class="sd">        matrix (lattice matrix, n by 3 array/matrix)</span>

<span class="sd">    Return:</span>
<span class="sd">        cubic symmetric eqivalents of the list of vectors.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">sym_group</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">([</span><span class="mi">24</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">])</span>
    <span class="n">sym_group</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="p">:]</span> <span class="o">=</span> <span class="p">[[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">]]</span>
    <span class="n">sym_group</span><span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="p">:]</span> <span class="o">=</span> <span class="p">[[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">]]</span>
    <span class="n">sym_group</span><span class="p">[</span><span class="mi">2</span><span class="p">,</span> <span class="p">:]</span> <span class="o">=</span> <span class="p">[[</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">]]</span>
    <span class="n">sym_group</span><span class="p">[</span><span class="mi">3</span><span class="p">,</span> <span class="p">:]</span> <span class="o">=</span> <span class="p">[[</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">]]</span>
    <span class="n">sym_group</span><span class="p">[</span><span class="mi">4</span><span class="p">,</span> <span class="p">:]</span> <span class="o">=</span> <span class="p">[[</span><span class="mi">0</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">]]</span>
    <span class="n">sym_group</span><span class="p">[</span><span class="mi">5</span><span class="p">,</span> <span class="p">:]</span> <span class="o">=</span> <span class="p">[[</span><span class="mi">0</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">]]</span>
    <span class="n">sym_group</span><span class="p">[</span><span class="mi">6</span><span class="p">,</span> <span class="p">:]</span> <span class="o">=</span> <span class="p">[[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">]]</span>
    <span class="n">sym_group</span><span class="p">[</span><span class="mi">7</span><span class="p">,</span> <span class="p">:]</span> <span class="o">=</span> <span class="p">[[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">]]</span>
    <span class="n">sym_group</span><span class="p">[</span><span class="mi">8</span><span class="p">,</span> <span class="p">:]</span> <span class="o">=</span> <span class="p">[[</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">]]</span>
    <span class="n">sym_group</span><span class="p">[</span><span class="mi">9</span><span class="p">,</span> <span class="p">:]</span> <span class="o">=</span> <span class="p">[[</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">]]</span>
    <span class="n">sym_group</span><span class="p">[</span><span class="mi">10</span><span class="p">,</span> <span class="p">:]</span> <span class="o">=</span> <span class="p">[[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">]]</span>
    <span class="n">sym_group</span><span class="p">[</span><span class="mi">11</span><span class="p">,</span> <span class="p">:]</span> <span class="o">=</span> <span class="p">[[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">]]</span>
    <span class="n">sym_group</span><span class="p">[</span><span class="mi">12</span><span class="p">,</span> <span class="p">:]</span> <span class="o">=</span> <span class="p">[[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">],</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">]]</span>
    <span class="n">sym_group</span><span class="p">[</span><span class="mi">13</span><span class="p">,</span> <span class="p">:]</span> <span class="o">=</span> <span class="p">[[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">],</span> <span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">]]</span>
    <span class="n">sym_group</span><span class="p">[</span><span class="mi">14</span><span class="p">,</span> <span class="p">:]</span> <span class="o">=</span> <span class="p">[[</span><span class="mi">0</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">],</span> <span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">]]</span>
    <span class="n">sym_group</span><span class="p">[</span><span class="mi">15</span><span class="p">,</span> <span class="p">:]</span> <span class="o">=</span> <span class="p">[[</span><span class="mi">0</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">],</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">]]</span>
    <span class="n">sym_group</span><span class="p">[</span><span class="mi">16</span><span class="p">,</span> <span class="p">:]</span> <span class="o">=</span> <span class="p">[[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">],</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">]]</span>
    <span class="n">sym_group</span><span class="p">[</span><span class="mi">17</span><span class="p">,</span> <span class="p">:]</span> <span class="o">=</span> <span class="p">[[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">],</span> <span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">]]</span>
    <span class="n">sym_group</span><span class="p">[</span><span class="mi">18</span><span class="p">,</span> <span class="p">:]</span> <span class="o">=</span> <span class="p">[[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">],</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">]]</span>
    <span class="n">sym_group</span><span class="p">[</span><span class="mi">19</span><span class="p">,</span> <span class="p">:]</span> <span class="o">=</span> <span class="p">[[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">],</span> <span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">]]</span>
    <span class="n">sym_group</span><span class="p">[</span><span class="mi">20</span><span class="p">,</span> <span class="p">:]</span> <span class="o">=</span> <span class="p">[[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">]]</span>
    <span class="n">sym_group</span><span class="p">[</span><span class="mi">21</span><span class="p">,</span> <span class="p">:]</span> <span class="o">=</span> <span class="p">[[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">]]</span>
    <span class="n">sym_group</span><span class="p">[</span><span class="mi">22</span><span class="p">,</span> <span class="p">:]</span> <span class="o">=</span> <span class="p">[[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">]]</span>
    <span class="n">sym_group</span><span class="p">[</span><span class="mi">23</span><span class="p">,</span> <span class="p">:]</span> <span class="o">=</span> <span class="p">[[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">]]</span>

    <span class="n">mat</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">atleast_2d</span><span class="p">(</span><span class="n">mat</span><span class="p">)</span>
    <span class="n">all_vectors</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">for</span> <span class="n">sym</span> <span class="ow">in</span> <span class="n">sym_group</span><span class="p">:</span>
        <span class="k">for</span> <span class="n">vec</span> <span class="ow">in</span> <span class="n">mat</span><span class="p">:</span>
            <span class="n">all_vectors</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">sym</span><span class="p">,</span> <span class="n">vec</span><span class="p">))</span>
    <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">unique</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">all_vectors</span><span class="p">),</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span></div>
</pre></div>

            <div class="clearer"></div>
          </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../../index.html">pymatgen 2020.7.3 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../../index.html" >Module code</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="../../../pymatgen.html" >pymatgen</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="">pymatgen.analysis.gb.grain</a></li> 
      </ul>
    </div>

    <div class="footer" role="contentinfo">
        &#169; Copyright 2011, Pymatgen Development Team.
      Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 3.1.2.
    </div>
<div class="footer">This page uses <a href="http://analytics.google.com/">
Google Analytics</a> to collect statistics. You can disable it by blocking
the JavaScript coming from www.google-analytics.com.
<script type="text/javascript">
  (function() {
    var ga = document.createElement('script');
    ga.src = ('https:' == document.location.protocol ?
              'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    ga.setAttribute('async', 'true');
    document.documentElement.firstChild.appendChild(ga);
  })();
</script>
</div>

  </body>
</html>